Files
XMen/Assets/Scripts/MRCamera.cs
2025-07-02 17:56:55 +08:00

61 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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));
}
}
}