Unity摄像机上下旋转镜头实现

该博客介绍了如何在Unity3D中实现摄像机的上下顶点锁定及连续旋转功能。通过编写脚本,实现了当鼠标右键拖动时,摄像机在限定范围内围绕目标对象上下旋转,同时确保不会超过预设的俯仰角度限制。此外,还提供了两种不同的摄像机平滑旋转方法,确保摄像机在接近顶点时不会发生快速翻转或不连续的现象。
摘要由CSDN通过智能技术生成

Unity摄像机上下旋转镜头实现

1.上下顶点锁定

将摄像机放在某对象下面作为子类
父类左右旋转,摄像机上下旋转
在这里插入图片描述

public class MoveCameraAlpha : MonoBehaviour
{
    [Header("旋转速度")]
    [Range(0,1)]
    public float turnSpeed;
    //Main Camera的父类
    private Transform _cameraFather;
    
    private float _mousex;
    private float _mousey;
    
    private float xAxisClamp;

    private void Awake()
    {
        _cameraFather = transform.root;
    }
    

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        xAxisClamp = 0.0f;
    }

    private void Update()
    {
        if (Input.GetMouseButton(1))
        {
            _mousex = Input.GetAxis("Mouse X");
            _mousey = Input.GetAxis("Mouse Y");
            
            xAxisClamp += _mousey;
            //Debug.Log(xAxisClamp);

            if (xAxisClamp > 90.0f)
            {
                xAxisClamp = 90.0f;
                _mousey = 0.0f;
                ClampXAxisRotationToValue(270.0f);
            }
            else if (xAxisClamp < -90.0f)
            {
                xAxisClamp = -90.0f;
                _mousey = 0.0f;
                ClampXAxisRotationToValue(90.0f);
            }
            
            _cameraFather.Rotate(Vector3.up * _mousex);
            transform.Rotate(Vector3.left * _mousey);
            
            Vector3 res = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
            transform.eulerAngles = res;
        }
        
    }
    
    private void ClampXAxisRotationToValue(float value)
    {
        Vector3 eulerRotation = transform.eulerAngles;
        eulerRotation.x = value;
        transform.eulerAngles = eulerRotation;
    }
}

public class MoveCameraAlpha : MonoBehaviour
{
    [Header("旋转速度")]
    [Range(0,1)]
    public float turnSpeed;
    //Main Camera的父类
    private Transform _cameraFather;
    
    private float _mousex;
    private float _mousey;
    
    private float xAxisClamp;

    private void Awake()
    {
        _cameraFather = transform.root;
    }
    

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        xAxisClamp = 0.0f;
    }

    private void Update()
    {
        if (Input.GetMouseButton(1))
        {
            _mousex = Input.GetAxis("Mouse X");
            _mousey = Input.GetAxis("Mouse Y");
            
            xAxisClamp -= _mousey;
            //Debug.Log(xAxisClamp);

           xAxisClamp = Mathf.Clamp(xAxisClmap,-90f,90f);
            
            _cameraFather.Rotate(Vector3.up * _mousex);
            transform.localRotation = Quaternion.Euler(xAxisClamp,0f,0f);
        }
    }
}

2.镜头上下了连续旋转

public class MoveCamera : MonoBehaviour
{
    [Header("移动速度")]
    public float moveSpeed;
    [Header("旋转速度")]
    [Range(0,1)]
    public float turnSpeed;
    private float _hor;
    private float _ver;
    
    private float _mousex;
    private float _mousey;
    
    private float _yaw;
    private float _pitch;
    
    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        _yaw = transform.eulerAngles.y;
        _pitch = transform.eulerAngles.x;
    }

    private void Update()
    {
        if (Input.GetMouseButton(1))
        {
            _hor = Input.GetAxis("Horizontal");
            _ver = Input.GetAxis("Vertical");
            transform.position += _hor * transform.right * Time.deltaTime * moveSpeed + 
                                  _ver * transform.forward * Time.deltaTime * moveSpeed;
        
            _mousex = Input.GetAxis("Mouse X");
            _mousey = Input.GetAxis("Mouse Y");

            //方法一,到顶点会快速水平旋转
            // transform.Rotate(-_mousey, _mousex, 0);
            // Vector3 res = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);
            // transform.eulerAngles = res;
            
            //方法二,没问题的实现
            _yaw = (_yaw + _mousex) % 360f;
            _pitch = (_pitch - _mousey) % 360f;
            
            transform.rotation = Quaternion.AngleAxis(_yaw, Vector3.up) * Quaternion.AngleAxis(_pitch, Vector3.right);
            
            //方法三,到顶点会水平旋转180度
            // transform.forward = Quaternion.AngleAxis(_mousex, Vector3.up) * transform.forward;
            // transform.forward = Quaternion.AngleAxis(-_mousey, Vector3.right) * transform.forward;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值