Unity 鼠标控制相机移动旋转

21 篇文章 0 订阅
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
[SerializeField]
public enum RotateController { KeyControllerRotate, MouseControllerRotate };
public class FirstPersonController : MonoBehaviour
{

    public byte priority = 13;                                       //相机的优先级
    public CinemachineVirtualCamera CM_FirstPerson;                 //虚拟相机
    public RotateController rotateController;                       //键盘控制旋转还是鼠标控制旋转
    float moveH, moveV, rotaH, rotaV;                               //键盘与鼠标的移动增量
    public float moveSpeed, RotaSpeed;                              //移动速度,旋转速度
    public bool CanRotate = true;                                   //是否允许旋转
    public int maxAngle;                                            //上下旋转的最大角度   
	private Vector3 resetPosition;

    void Start()
    {
        
        if (CM_FirstPerson != null)
        {
            CM_FirstPerson.Priority = priority;
        }    
        resetPosition = transform.position;    
    }

    // Update is called once per frame
    void Update()
    {

        Move();
        if (CanRotate) {
            Rota();                     //旋转
            AngleLimit(maxAngle);       //旋转角度限制
            }
        //单击鼠标右键开启关闭旋转操作
        if (Input.GetMouseButtonDown(1))
        {
            CanRotate = !CanRotate;
        }
    } 
    /// <summary>
    /// 移动控制
    /// </summary>
    void Move()
    {
        moveH = Input.GetAxis("Horizontal");
        moveV = Input.GetAxis("Vertical");
        //如果按住ctrl 那就不进行移动
        if (Input.GetKey(KeyCode.LeftControl))
        {
            return;
        }
        if (moveH != 0 || moveV != 0)
        {
            transform.Translate(moveH * moveSpeed * Time.deltaTime, 0, moveV * moveSpeed * Time.deltaTime);
            transform.position = new Vector3(transform.position.x, resetPosition.y, transform.position.z);    //锁定y轴的位置不变
        }
    }
    /// <summary>
    /// 判断是使用鼠标控制旋转还是键盘控制旋转
    /// </summary>
    void Rota()
    {


        if (rotateController == RotateController.KeyControllerRotate)
        {
            keyRotate();         //键盘控制
        }
        else
        {
            mouseRotate();              //鼠标控制
        }

    }

    /// <summary>
    /// 键盘控制旋转
    /// </summary>
    void keyRotate()
    {
      
        if (Input.GetKey(KeyCode.LeftControl))
        {          
            transform.Rotate(Vector3.up * moveH * RotaSpeed * Time.deltaTime, Space.World); //沿世界坐标的Y轴旋转
            transform.Rotate(Vector3.right * -moveV * RotaSpeed * 0.7f * Time.deltaTime);
        }

    }
    /// <summary>
    /// 鼠标旋转
    /// </summary>
    void mouseRotate()
    {
        rotaH = Input.GetAxis("Mouse X");
        rotaV = Input.GetAxis("Mouse Y");
        transform.Rotate(Vector3.up * rotaH * RotaSpeed * Time.deltaTime, Space.World);
        transform.Rotate(Vector3.right * -rotaV * RotaSpeed * 0.7f * Time.deltaTime);
    }
/// <summary>
/// 角度限制
/// </summary>
    void AngleLimit(int maxAngle){
        if(transform.localEulerAngles.x > maxAngle && transform.localEulerAngles.x < 180){
            transform.localEulerAngles = new Vector3(maxAngle,transform.localEulerAngles.y,0);
        }
        if(transform.localEulerAngles.x < (360-maxAngle) && transform.localEulerAngles.x  > 180){
            transform.localEulerAngles = new Vector3(360-maxAngle,transform.localEulerAngles.y,0);
        }
    }
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值