当前位置:网站首页>unity摄像机旋转带有滑动效果(自转)
unity摄像机旋转带有滑动效果(自转)
2022-04-23 04:41:00 【龙胖胖的博客】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Cameratest : MonoBehaviour {
public static Cameratest instance;
private void Awake()
{
instance = this;
}
public float sensitivity = 4.0f;
[HideInInspector]
public float sensitivityAmt = 4.0f;
private float minimumX = -360f;
private float maximumX = 360f;
private float minimumY = -85f;
private float maximumY = 85f;
[HideInInspector]
public float rotationX = 0.0f;
[HideInInspector]
public float rotationY = 0.0f;
[HideInInspector]
public float inputY = 0.0f;
public float smoothSpeed = 0.35f;
private Quaternion originalRotation;
private Transform myTransform;
[HideInInspector]
public float recoilX;
[HideInInspector]
public float recoilY;
//判断是否需要旋转
private bool m_IsonClickUI;
//摄像机的视角大小放大缩小功能
private float m_cameraFieldOfMask=60;
void Start()
{
myTransform = transform;
originalRotation = myTransform.localRotation;
Vector3 tempRotation = new Vector3(0, Camera.main.transform.eulerAngles.y, 0);
originalRotation.eulerAngles = tempRotation;
sensitivityAmt = sensitivity;
}
void Update()
{
Ispointetovergameobject(Input.mousePosition);
RotationCamera();
ZoomInOut();
}
/// <summary>
/// 初始化相机角度
/// </summary>
/// <param name="Angle_x"></param>
/// <param name="Angle_y"></param>
public void InitRot(float Angle_x,float Angle_y)
{
rotationX = Angle_x;
rotationY = Angle_y;
}
/// <summary>
/// 相机旋转
/// </summary>
void RotationCamera()
{
if (!m_IsonClickUI)
{
if (Input.GetMouseButton(0))
{
Cursor.visible = false;
if (Time.timeScale > 0 && Time.deltaTime > 0)
{
rotationX += Input.GetAxisRaw("Mouse X") * -sensitivityAmt * Time.timeScale;
rotationY += Input.GetAxisRaw("Mouse Y") * -sensitivityAmt * Time.timeScale;
//reset vertical recoilY value if it would exceed maximumY amount
if (maximumY - Input.GetAxisRaw("Mouse Y") * sensitivityAmt * Time.timeScale < recoilY)
{
rotationY += recoilY;
recoilY = 0.0f;
}
//reset horizontal recoilX value if it would exceed maximumX amount
if (maximumX - Input.GetAxisRaw("Mouse X") * sensitivityAmt * Time.timeScale < recoilX)
{
rotationX += recoilX;
recoilX = 0.0f;
}
rotationX = ClampAngle(rotationX, minimumX, maximumX);
rotationY = ClampAngle(rotationY, minimumY - recoilY, maximumY - recoilY);
inputY = rotationY + recoilY;
}
}
else
{
Cursor.visible = true;
}
}
if (Input.GetMouseButtonUp(0))
{
m_IsonClickUI = false;
}
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX + recoilX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY + recoilY, -Vector3.right);
// myTransform.rotation = Quaternion.Slerp(myTransform.rotation, originalRotation * xQuaternion * yQuaternion, smoothSpeed * Time.smoothDeltaTime * 60 / Time.timeScale);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, originalRotation * xQuaternion * yQuaternion, smoothSpeed * Time.deltaTime);
myTransform.rotation = Quaternion.Euler(myTransform.rotation.eulerAngles.x, myTransform.rotation.eulerAngles.y, 0.0f);
}
/// <summary>
/// 限制旋转角度
/// </summary>
/// <param name="angle"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static float ClampAngle(float angle, float min, float max)
{
angle = angle % 360;
if ((angle >= -360F) && (angle <= 360F))
{
if (angle < -360F)
{
angle += 360F;
}
if (angle > 360F)
{
angle -= 360F;
}
}
return Mathf.Clamp(angle, min, max);
}
/// <summary>
/// 放大缩小
/// </summary>
void ZoomInOut()
{
//鼠标放大缩小功能
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
m_cameraFieldOfMask = Mathf.Clamp(m_cameraFieldOfMask += 2, 30, 60);
Camera.main.fieldOfView = m_cameraFieldOfMask;
}
else if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
m_cameraFieldOfMask = Mathf.Clamp(m_cameraFieldOfMask -= 2, 30, 60);
Camera.main.fieldOfView = m_cameraFieldOfMask;
}
}
/// <summary>
/// 判断是否点击到UI
/// </summary>
/// <param name="input"></param>
void Ispointetovergameobject(Vector2 input)
{
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = input;
List<RaycastResult> rayresults = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, rayresults);
if (Input.GetMouseButtonDown(0))
{
for (int i = 0; i < rayresults.Count; i++)
{
if (rayresults[i].gameObject.tag == "ui")
{
m_IsonClickUI = true;
}
}
}
}
}
复制脚本放到摄像机上即可
版权声明
本文为[龙胖胖的博客]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qiao2037641855/article/details/106564017
边栏推荐
- Leetcode008 -- implement strstr() function
- 229. 求众数 II
- 383. 赎金信
- 520.检测大写字母
- Effects of antibiotics on microbiome and human health
- Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
- Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
- Installation du compilateur croisé de la plateforme zynq
- A heavy sword without a blade is a great skill
- Go反射—Go语言圣经学习笔记
猜你喜欢
Apache Bench(ab 压力测试工具)的安装与使用
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
Interaction of diet gut microbiota on cardiovascular disease
那些年我面试过的Android开发岗总结(附面试题+答案解析)
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
Supplement: Annotation
无线键盘全国产化电子元件推荐方案
Recursive call -- Enumeration of permutations
Effects of antibiotics on microbiome and human health
Understand the gut organ axis, good gut and good health
随机推荐
兼容NSR20F30NXT5G的小体积肖特基二极管
Migrate from MySQL database to AWS dynamodb
2019 is coming to an end, the longest day.
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
Mysql---数据读写分离、多实例
Last day of 2017
229. 求众数 II
Coinbase:关于跨链桥的基础知识、事实和统计数据
针对NFT的网络钓鱼
QML进阶(四)-绘制自定义控件
What is a data island? Why is there still a data island in 2022?
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
The perfect combination of collaborative process and multi process
Leetcode006 -- find the longest common prefix in the string array
test
Logger and zap log Library in go language
AWS eks add cluster user or Iam role
重剑无锋,大巧不工
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
A heavy sword without a blade is a great skill