unity 滑鼠360度旋轉攝像機,帶角度限制
public Transform target;//相機的目標
private float xSpeed = 200;//x軸的旋轉速度
private float ySpeed = 150;//x軸的旋轉速度
public float yMin = -20;//y最小角度
public float yMax = 20;//y最大角度
public bool needDamping = true; //是否需要的阻尼
private float damping = 5;//阻尼
private float x = 0;
private float y = 0;
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
void LateUpdate()
{
if (target)
{
//使用滑鼠光按鈕來控制相機,調整照相機位置
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMin, yMax);
}
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 disVector = new Vector3(0.0f, 0.0f, 0);
Vector3 position = rotation * disVector + target.position;
if (needDamping)
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
}
else
{
transform.rotation = rotation;
transform.position = position;
}
}
}
//限制旋轉角度
private float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
private void Update()
{
//視野放大縮小
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (GetComponent<Camera>().fieldOfView >= 30)
{
GetComponent<Camera>().fieldOfView--;
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (GetComponent<Camera>().fieldOfView <= 80)
{
GetComponent<Camera>().fieldOfView++;
}
}
}