当前位置:网站首页>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
边栏推荐
- 指纹Key全国产化电子元件推荐方案
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
- Recursive call -- Enumeration of permutations
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- What is the thirty-six plan
- Code007 -- determine whether the string in parentheses matches
- leetcode004--罗马数字转整数
- The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
- AWS EKS 部署要点以及控制台与eksctl创建的差异
- Mysql50 basic exercises
猜你喜欢
![[paper reading] [3D object detection] voxel transformer for 3D object detection](/img/a2/9f66789cc12fad99491309717cf418.png)
[paper reading] [3D object detection] voxel transformer for 3D object detection

Detailed explanation of life cycle component of jetpack

IDE idea automatic compilation and configuration of on update action and on frame deactivation

win10, mysql-8.0.26-winx64.zip 安装

Small volume Schottky diode compatible with nsr20f30nxt5g

Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition

Installation of zynq platform cross compiler

Use recyclerview to realize left-right side-by-side classification selection

数据孤岛是什么?为什么2022年仍然存在数据孤岛?
![[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022](/img/48/0e95841743bada4faf3edfee31cb6a.png)
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
随机推荐
MYSQL50道基础练习题
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
Apache Bench(ab 压力测试工具)的安装与使用
三十六计是什么
兼容NSR20F30NXT5G的小体积肖特基二极管
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
leetcode001--返回和为target的数组元素的下标
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
What is a data island? Why is there still a data island in 2022?
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
The perfect combination of collaborative process and multi process
383. Ransom letter
A lifetime of needs, team collaboration can play this way on cloud nailing applet
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
Huawei machine test -- high precision integer addition
Go reflection rule
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection