Unity 脚本 | Unity相机控制脚本(鼠标键盘控制,简单实用)

Unity 脚本 | Unity相机控制脚本(鼠标键盘控制,简单实用)

王先生
2024-06-06 / 0 评论 / 11 阅读 / 正在检测是否收录...

unity相机控制脚本,思路是仿照Unity Scene 里面控制的逻辑

鼠标滚轮 上滚 拉近视野(放大),下滚 推远视野(缩小)
鼠标滚轮 按下并拖动,调整相机位置
鼠标右键 按下并拖动,调整相机视角
左键留空
w a s d 类似于 fps 游戏一样前后左右移动
ctrl 向下移动
空格 向上移动

该脚本直接挂载在相机上就可以用

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class MouseOperationCameraRotationFovMove : MonoBehaviour
{
    public enum MouseState { 
        None,
        MidMouseBtn,
        LeftMouseBtn
    }
 
 
 
    private MouseState mMouseState = MouseState.None;
    private Camera mCamera;
 
 
    private void Awake()
    {
        mCamera = GetComponent<Camera>();
        if (mCamera==null)
        {
            UnityEngine.Debug.LogError(GetType()+"camera Get Error ……");
        }
 
        GetDefaultFov();
    }
 
    private void LateUpdate()
    {
        CameraRotate();
 
        CameraFOV();
 
        CameraMove();
    }
 
    #region Camera Rotation
 
    //旋转最大角度
    public int yRotationMinLimit = -90;
    public int yRotationMaxLimit = 0;
    //旋转速度
    public float xRotationSpeed = 250.0f;
    public float yRotationSpeed = 120.0f;
    //旋转角度
    private float xRotation = 0.0f;
    private float yRotation = 0.0f;
 
    /// <summary>
    /// 鼠标右间点下移动进行旋转
    /// </summary>
    void CameraRotate()
    {
        if (Input.GetMouseButton(1))    // 0:鼠标左键;1:鼠标右键;2:鼠标中键
        {
            if (mMouseState == MouseState.None)
            {
                // 保存当前的旋转角度,默认为0,0的时候代表第一次,获取一下
                Vector3 currentRotation = transform.rotation.eulerAngles;
                if (xRotation == 0.0f && yRotation == 0.0f)
                {
                    // 暂时不知道为啥X和Y要反过来
                    yRotation = -currentRotation.x;
                    xRotation = 360 - currentRotation.y;
                }
                //Input.GetAxis("MouseX")获取鼠标移动的X轴的距离
                xRotation -= Input.GetAxis("Mouse X") * xRotationSpeed * 0.02f;
                yRotation += Input.GetAxis("Mouse Y") * yRotationSpeed * 0.02f;
                yRotation = ClampValue(yRotation, yRotationMinLimit, yRotationMaxLimit);//这个函数在结尾
                                                                                        //欧拉角转化为四元数
                Quaternion rotation = Quaternion.Euler(-yRotation, -xRotation, 0);
                transform.rotation = rotation;
            }
        }
    }
 
    #endregion
 
    #region Camera fov
 
    //fov 最大最小角度
    public int fovMinLimit = 25;
    public int fovMaxLimit = 75;
    //fov 变化速度
    public float fovSpeed = 50.0f;
    //fov 角度
    private float fov = 0.0f;
 
    void GetDefaultFov() {
        fov = mCamera.fieldOfView;
    }
 
    /// <summary>
    /// 滚轮控制相机视角缩放
    /// </summary>
    public void CameraFOV()
    {
        //获取鼠标滚轮的滑动量
        fov -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * 100 * fovSpeed;
 
        // fov 限制修正
        fov = ClampValue(fov,fovMinLimit, fovMaxLimit);
 
        //改变相机的 fov
        mCamera.fieldOfView = (fov);
    }
 
    #endregion
 
 
    #region Camera Move
 
    float _mouseX = 0;
    float _mouseY = 0;
    public float moveSpeed = 10;
    /// <summary>
    /// 中键控制拖动
    /// </summary>
    public void CameraMove()
    {
        if (Input.GetMouseButton(2))    // 0:鼠标左键;1:鼠标右键;2:鼠标中键
        {
            _mouseX = Input.GetAxis("Mouse X");
            _mouseY = Input.GetAxis("Mouse Y");
 
            //相机位置的偏移量(Vector3类型,实现原理是:向量的加法)
            Vector3 moveDir = (_mouseX * - transform.right + _mouseY * - transform.forward);
 
            //限制y轴的偏移量
            moveDir.y = 0;
            transform.position += moveDir * 0.5f * moveSpeed;
        }
        else if (Input.GetMouseButtonDown(2))
        {
            mMouseState = MouseState.MidMouseBtn;
            UnityEngine.Debug.Log(GetType() + "mMouseState = " + mMouseState.ToString());
        }
        else if (Input.GetMouseButtonUp(2))
        {
            mMouseState = MouseState.None;
            UnityEngine.Debug.Log(GetType() + "mMouseState = " + mMouseState.ToString()); 
        }



        // WASD控制相机的前后左右移动
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 direction = new Vector3(horizontalInput, 0, verticalInput).normalized;
        transform.Translate(direction * moveSpeed * Time.deltaTime);

        // Ctrl键控制相机向下移动
        if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
        {
            transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);
        }

        // 空格键控制相机向上移动
        if (Input.GetKey(KeyCode.Space))
        {
            transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
        }

    }
 
    #endregion
 
    #region tools ClampValue
 
    //值范围值限定
    float ClampValue(float value, float min, float max)//控制旋转的角度
    {
        if (value < -360)
            value += 360;
        if (value > 360)
            value -= 360;
        return Mathf.Clamp(value, min, max);//限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
    }
 
 
    #endregion
}

评论 (0)

取消