unity中使用鼠标右键控制旋转滑轮控制视角

13 篇文章 0 订阅
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class MouseCamera : MonoBehaviour
{
    public enum RotationAxes
    {
        MouseXAndY = 0, //上下左右滑动
        MouseX = 1, //左右滑动
        MouseY = 2  //上下滑动
    }

    public RotationAxes m_axes = RotationAxes.MouseXAndY;
    [Range(1,10)]
    public int m_sensitivityX = 5; //视角转动的幅度
    [Range(1,10)]
    public int m_sensitivityY = 5; 
    [Range(1,10)]
    public int m_fieldOfView = 5; //视角拉伸的幅度
    // 垂直方向的镜头转向范围 
    public float m_minimumY = -45f;
    public float m_maximumY = 45f;

    float m_rotationY = 0f;

    void Start()
    {
       
    }
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            if (m_axes == RotationAxes.MouseXAndY)
            {
                float m_rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * m_sensitivityX;
                m_rotationY += Input.GetAxis("Mouse Y") * m_sensitivityY;
                m_rotationY = Mathf.Clamp(m_rotationY, m_minimumY, m_maximumY);

                transform.localEulerAngles = new Vector3(-m_rotationY, m_rotationX, 0);
            }
            else if (m_axes == RotationAxes.MouseX)
            {
                transform.Rotate(0, Input.GetAxis("Mouse X") * m_sensitivityX, 0);
            }
            else
            {
                m_rotationY += Input.GetAxis("Mouse Y") * m_sensitivityY;
                m_rotationY = Mathf.Clamp(m_rotationY, m_minimumY, m_maximumY);

                transform.localEulerAngles = new Vector3(-m_rotationY, transform.localEulerAngles.y, 0);
            }
        }
        //通过鼠标滚轮放大 和 缩放视角
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            Camera.main.fieldOfView += m_fieldOfView;
        }
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            Camera.main.fieldOfView -= m_fieldOfView;
        }
    }
}
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值