Unity3D拖拽相机缓动并限制最大角度松手回正

文章描述了一个在Unity中实现的MouseLookCamera脚本,支持鼠标和触摸屏两种交互方式控制相机的旋转,包括平滑移动和重置功能。
摘要由CSDN通过智能技术生成

适用于一体机待机界面的交互形式
在这里插入图片描述
编写了鼠标和触摸屏两种方式,脚本挂在相机上,相机要有一个父物体

using UnityEngine;
using DG.Tweening;

public class MouseLookCamera : MonoBehaviour
{
    public bool isMouse = false;

    public float sensitivityMouseX = 100F;
    public float sensitivityMouseY = 100F;
    public float sensitivityTouchX = 5F;
    public float sensitivityTouchY = 5F;
    private float minimumX = -5F;
    private float maximumX = 5F;
    private float minimumY = -5F;
    private float maximumY = 5F;

    public float smoothSpeedMouse = 3f;
    public float smoothSpeedTouch = 30f;

    private Transform playerBody;
    [SerializeField]
    private float rotationX = 0F;
    [SerializeField]
    private float rotationY = 0F;

    private Tweener tweener1;
    private Tweener tweener2;
    void Start()
    {
        playerBody = transform.parent;
    }

    void Update()
    {
        if (isMouse)
        {
            if (Input.GetMouseButtonDown(0))
            {
                tweener1.Kill();
                tweener2.Kill();
                rotationX = transform.localEulerAngles.x;
                if (rotationX > 180) rotationX -= 360;
                rotationY = playerBody.transform.localEulerAngles.y;
                if (rotationY > 180) rotationY -= 360;
            }
            if (Input.GetMouseButton(0))
            {
                float mouseX = Input.GetAxis("Mouse X") * sensitivityMouseX * Time.deltaTime;
                float mouseY = Input.GetAxis("Mouse Y") * sensitivityMouseY * Time.deltaTime;

                rotationX += mouseY;
                rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
                rotationY -= mouseX;
                rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

                Quaternion targetRotationA = Quaternion.Euler(rotationX, 0, 0);
                Quaternion targetRotationB = Quaternion.Euler(0, rotationY, 0);

                transform.localRotation = Quaternion.Lerp(transform.localRotation, targetRotationA, smoothSpeedMouse * Time.deltaTime);
                playerBody.localRotation = Quaternion.Lerp(playerBody.transform.localRotation, targetRotationB, smoothSpeedMouse * Time.deltaTime);
            }
            if (Input.GetMouseButtonUp(0))
            {
                ResetCamera();
            }
        }
        else
        {
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                tweener1.Kill();
                tweener2.Kill();
                rotationX = transform.localEulerAngles.x;
                if (rotationX > 180) rotationX -= 360;
                rotationY = playerBody.transform.localEulerAngles.y;
                if (rotationY > 180) rotationY -= 360;
            }
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                Touch touch = Input.GetTouch(0);

                float touchSwipeX = touch.deltaPosition.x  * sensitivityTouchX * Time.deltaTime;
                float touchSwipeY = touch.deltaPosition.y  * sensitivityTouchY * Time.deltaTime;

                rotationX += touchSwipeY;
                rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
                rotationY -= touchSwipeX;
                rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

                Quaternion targetRotationA = Quaternion.Euler(rotationX, 0, 0);
                Quaternion targetRotationB = Quaternion.Euler(0, rotationY, 0);

                transform.localRotation = Quaternion.Lerp(transform.localRotation, targetRotationA, smoothSpeedTouch * Time.deltaTime);
                playerBody.localRotation = Quaternion.Lerp(playerBody.transform.localRotation, targetRotationB, smoothSpeedTouch * Time.deltaTime);
            }
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                ResetCamera();
            }
        }
    }
    private void ResetCamera()
    {
        tweener1.Kill();
        tweener2.Kill();
        tweener1 = transform.DOLocalRotateQuaternion(Quaternion.Euler(Vector3.zero), 0.5f);
        tweener2 = playerBody.transform.DOLocalRotateQuaternion(Quaternion.Euler(Vector3.zero), 0.5f);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值