61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
using UnityEngine;
|
||
|
||
public class MRCamera : MonoBehaviour
|
||
{
|
||
private float xRotation;
|
||
private float yRotation;
|
||
public int yRotationMinLimit = -20;
|
||
public int yRotationMaxLimit = 80;
|
||
private float xRotationSpeed = 100;
|
||
private float yRotationSpeed = 100;
|
||
|
||
void Update()
|
||
{
|
||
CameraRotation();
|
||
CameraMove();
|
||
}
|
||
|
||
float ClampValue(float value, float min, float max)//控制旋转的角度
|
||
{
|
||
if (value < -360)
|
||
value += 360;
|
||
if (value > 360)
|
||
value -= 360;
|
||
return Mathf.Clamp(value, min, max);//限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
|
||
}
|
||
|
||
void CameraRotation()
|
||
{
|
||
float _MouseX = Input.GetAxis("Mouse X");
|
||
float _MouseY = Input.GetAxis("Mouse Y");
|
||
if (Input.GetMouseButton(1))
|
||
{
|
||
xRotation -= _MouseX * xRotationSpeed * 0.02f;
|
||
yRotation += _MouseY * yRotationSpeed * 0.02f;
|
||
yRotation = ClampValue(yRotation, yRotationMinLimit, yRotationMaxLimit);
|
||
Quaternion rotation = Quaternion.Euler(-yRotation, -xRotation, 0);
|
||
transform.rotation = rotation;
|
||
}
|
||
}
|
||
|
||
void CameraMove()
|
||
{
|
||
if (Input.GetKey(KeyCode.W))
|
||
{
|
||
transform.Translate(new Vector3(0, 0, 0.5f));
|
||
}
|
||
if (Input.GetKey(KeyCode.A))
|
||
{
|
||
transform.Translate(new Vector3(-0.5f, 0, 0));
|
||
}
|
||
if (Input.GetKey(KeyCode.S))
|
||
{
|
||
transform.Translate(new Vector3(0, 0, -0.5f));
|
||
}
|
||
if (Input.GetKey(KeyCode.D))
|
||
{
|
||
transform.Translate(new Vector3(0.5f, 0, 0));
|
||
}
|
||
}
|
||
}
|