概述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraMove : MonoBehaviour
{
public enum RotationAxes
{
MouseXAndY = 0,
MouseX = 1,
MouseY = 2
}
public RotationAxes m_axes = RotationAxes.MouseXAndY;
public float m_sensitivityX = 10f;
public float m_sensitivityY = 10f;
public bool CanMove = false;
// 水平方向的 镜头转向
public float m_minimumX = -360f;
public float m_maximumX = 360f;
// 垂直方向的 镜头转向 (这里给个限度 最大仰角为45°)
public float m_minimumY = -45f;
public float m_maximumY = 45f;
float MouseWheelSensitivity = 5;
float MouseZoomMin = 2;
float MouseZoomMax = 10;
float m_rotationY = 0f;
// Use this for initialization
void Start()
{
// 防止 刚体影响 镜头旋转
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (Camera.main.fieldOfView <= 200)
Camera.main.fieldOfView += 2;
}
//Zoom in
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (Camera.main.fieldOfView > 10)
Camera.main.fieldOfView -= 2;
}
if (Input.GetMouseButtonDown(1))
{
CanMove = !CanMove;
}
if (CanMove)
{
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);
}
}
}
}
最后
以上就是高兴台灯为你收集整理的Unity 鼠标滚轮缩放 拖动移动视角的全部内容,希望文章能够帮你解决Unity 鼠标滚轮缩放 拖动移动视角所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复