111 lines
2.7 KiB
C#
111 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
|
|
public class MouseOrbit : MonoBehaviour
|
|
{
|
|
public bool AllowRotation = true;
|
|
|
|
public bool clickToRotate;
|
|
|
|
public Transform target;
|
|
|
|
public float distance = 5f;
|
|
|
|
public float xSpeed = 120f;
|
|
|
|
public float ySpeed = 120f;
|
|
|
|
public float yMinLimit = -20f;
|
|
|
|
public float yMaxLimit = 80f;
|
|
|
|
public float distanceMin = 0.5f;
|
|
|
|
public float distanceMax = 15f;
|
|
|
|
public float XInit;
|
|
|
|
public float YInit = 60f;
|
|
|
|
private float x;
|
|
|
|
private float y;
|
|
|
|
public static bool rightclicked;
|
|
|
|
[HideInInspector]
|
|
public int i;
|
|
|
|
public void Start()
|
|
{
|
|
x = XInit;
|
|
y = YInit;
|
|
i = 0;
|
|
if ((bool)GetComponent<Rigidbody>())
|
|
{
|
|
GetComponent<Rigidbody>().freezeRotation = true;
|
|
}
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (AllowRotation)
|
|
{
|
|
rightclicked = Input.GetMouseButton(1);
|
|
}
|
|
if (i == 0)
|
|
{
|
|
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
|
|
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
|
|
y = ClampAngle(y, yMinLimit, yMaxLimit);
|
|
Quaternion quaternion = Quaternion.Euler(y, x, 0f);
|
|
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5f, distanceMin, distanceMax);
|
|
Vector3 vector = new Vector3(0f, 0f, 0f - distance);
|
|
Vector3 position = quaternion * vector + target.position;
|
|
base.transform.rotation = quaternion;
|
|
base.transform.position = position;
|
|
i = 1;
|
|
}
|
|
else if (clickToRotate)
|
|
{
|
|
if ((bool)target && rightclicked)
|
|
{
|
|
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
|
|
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
|
|
y = ClampAngle(y, yMinLimit, yMaxLimit);
|
|
Quaternion quaternion2 = Quaternion.Euler(y, x, 0f);
|
|
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5f, distanceMin, distanceMax);
|
|
Vector3 vector2 = new Vector3(0f, 0f, 0f - distance);
|
|
Vector3 position2 = quaternion2 * vector2 + target.position;
|
|
base.transform.rotation = quaternion2;
|
|
base.transform.position = position2;
|
|
}
|
|
}
|
|
else if ((bool)target && !rightclicked)
|
|
{
|
|
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
|
|
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
|
|
y = ClampAngle(y, yMinLimit, yMaxLimit);
|
|
Quaternion quaternion3 = Quaternion.Euler(y, x, 0f);
|
|
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5f, distanceMin, distanceMax);
|
|
Vector3 vector3 = new Vector3(0f, 0f, 0f - distance);
|
|
Vector3 position3 = quaternion3 * vector3 + target.position;
|
|
base.transform.rotation = quaternion3;
|
|
base.transform.position = position3;
|
|
}
|
|
}
|
|
|
|
public static float ClampAngle(float angle, float min, float max)
|
|
{
|
|
if (angle < -360f)
|
|
{
|
|
angle += 360f;
|
|
}
|
|
if (angle > 360f)
|
|
{
|
|
angle -= 360f;
|
|
}
|
|
return Mathf.Clamp(angle, min, max);
|
|
}
|
|
}
|