unity实现用鼠标右键控制摄像机视角上下左右移动
using System;
using System.Collections.Generic;
using UnityEngine;
public class ViewControl
{
enum RotationAxes
{
MouseXAndY,
MouseX,
MouseY
}
RotationAxes axes = RotationAxes.MouseXAndY;
float sensitivityX = 10;
float sensitivityY = 10;
float minimumY = -45;
float maximumY = 45;
private float rotationY = 0;
public void Update()
{
if (Input.GetMouseButton(0))
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = Camera.main.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
Camera.main.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
Camera.main.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
Camera.main.transform.localEulerAngles = new Vector3(-rotationY, Camera.main.transform.localEulerAngles.y, 0);
}
}
}
}