Unity的学习笔记(鼠标移动控制视角移动)

using UnityEngine;

public class MouseLook : MonoBehaviour {

    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } //定义一个枚举,移动xy,或者只是移动x,或者y
    public RotationAxes axes = RotationAxes.MouseXAndY;                 //声明一个枚举变量,方便在外面修改移动模式
    public float sensitivityX = 15f;                                    //定义一个移动速度
    public float sensitivityY = 15f;

    public float minimumY = -60f;       //定义俯视最低值,建议这个值,要不然会转过头
    public float maximumY = 60f;        //定义俯视最高值,建议这个值,要不然会转过头

    float rotationY = 0f;               //存储实际转动的Y值

    void Start()
    {

    }
    void Update ()
    {
        switch (axes)               //判断用户是用那种旋转方式
        {
            case RotationAxes.MouseXAndY:
                float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX; 

                rotationY += Input.GetAxis("Mouse Y") * sensitivityY; //
                rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

                transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
                break;
            case RotationAxes.MouseX:
                transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
                break;
            case RotationAxes.MouseY:
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

                transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
                break;
            default:
                break;
        }
    }
}

 

转载于:https://www.cnblogs.com/takanashi/p/11028423.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值