当前位置:网站首页>Unity camera rotation with sliding effect (rotation)
Unity camera rotation with sliding effect (rotation)
2022-04-23 04:41:00 【Longpangpang's blog】
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;
// Determine if rotation is needed
private bool m_IsonClickUI;
// Zoom in and out of the camera's viewing angle
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>
/// Initialize camera angle
/// </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>
/// Camera rotation
/// </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>
/// Limit rotation angle
/// </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>
/// Zoom in and out
/// </summary>
void ZoomInOut()
{
// Mouse zoom function
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>
/// Judge whether to click 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;
}
}
}
}
}
Copy the script and put it on the camera
版权声明
本文为[Longpangpang's blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230441112401.html
边栏推荐
- Installation of zynq platform cross compiler
- MySQL - data read / write separation, multi instance
- Basic operation of sequence table
- Summary of MySQL de duplication methods
- Leetcode003 -- judge whether an integer is a palindrome number
- Migrate from MySQL database to AWS dynamodb
- QML advanced (IV) - drawing custom controls
- Flink's important basics
- MYSQL50道基础练习题
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
猜你喜欢
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
Supplément: annotation
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
520.检测大写字母
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
补:注解(Annotation)
AWS EKS添加集群用户或IAM角色
Detailed explanation of life cycle component of jetpack
補:注解(Annotation)
随机推荐
Last day of 2017
Differences among electric drill, electric hammer and electric pick
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
Improving 3D object detection with channel wise transformer
leetcode001--返回和为target的数组元素的下标
FAQ of foreign lead and alliance Manager
2019 is coming to an end, the longest day.
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
2021数学建模国赛一等奖经验总结与分享
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
SQL statement for adding columns in MySQL table
Leetcode005 -- delete duplicate elements in the array in place
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
IEEE Transactions on industrial information (TII)
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
A lifetime of needs, team collaboration can play this way on cloud nailing applet
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
io. Platform. packageRoot; // ignore: deprecated_ Member_ use