summaryrefslogtreecommitdiff
path: root/MouseOrbitCS.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-03-13 11:00:58 +0800
committerchai <215380520@qq.com>2024-03-13 11:00:58 +0800
commit6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch)
treeb38119d2acf0a982cb67e381f146924b9bfc3b3f /MouseOrbitCS.cs
+init
Diffstat (limited to 'MouseOrbitCS.cs')
-rw-r--r--MouseOrbitCS.cs64
1 files changed, 64 insertions, 0 deletions
diff --git a/MouseOrbitCS.cs b/MouseOrbitCS.cs
new file mode 100644
index 0000000..40c2fb2
--- /dev/null
+++ b/MouseOrbitCS.cs
@@ -0,0 +1,64 @@
+using UnityEngine;
+
+public class MouseOrbitCS : MonoBehaviour
+{
+ public Transform target;
+
+ public float distance = 10f;
+
+ public float xSpeed = 250f;
+
+ public float ySpeed = 120f;
+
+ public float yMinLimit = -20f;
+
+ public float yMaxLimit = 80f;
+
+ private float x;
+
+ private float y;
+
+ private float normal_angle;
+
+ private void Start()
+ {
+ Vector3 eulerAngles = base.transform.eulerAngles;
+ x = eulerAngles.y;
+ y = eulerAngles.x;
+ }
+
+ private void LateUpdate()
+ {
+ if ((bool)target && !Input.GetKey(KeyCode.F))
+ {
+ if (!Input.GetMouseButton(0))
+ {
+ x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
+ y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
+ }
+ y = ClampAngle(y, yMinLimit + normal_angle, yMaxLimit + normal_angle);
+ Quaternion quaternion = Quaternion.Euler(y, x, 0f);
+ Vector3 position = quaternion * new Vector3(0f, 0f, 0f - distance) + target.position;
+ base.transform.rotation = quaternion;
+ base.transform.position = position;
+ }
+ }
+
+ private 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);
+ }
+
+ public void set_normal_angle(float a)
+ {
+ normal_angle = a;
+ }
+}