BFX_MouseOrbit.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. public class BFX_MouseOrbit : MonoBehaviour
  5. {
  6. public GameObject target;
  7. public float distance = 10.0f;
  8. public float xSpeed = 250.0f;
  9. public float ySpeed = 120.0f;
  10. public float yMinLimit = -20;
  11. public float yMaxLimit = 80;
  12. float x = 0.0f;
  13. float y = 0.0f;
  14. void Start()
  15. {
  16. var angles = transform.eulerAngles;
  17. x = angles.y;
  18. y = angles.x;
  19. }
  20. float prevDistance;
  21. void LateUpdate()
  22. {
  23. if (distance < 2) distance = 2;
  24. distance -= Input.GetAxis("Mouse ScrollWheel")*2;
  25. if (target && (Input.GetMouseButton(1)))
  26. {
  27. var pos = Input.mousePosition;
  28. var dpiScale = 1f;
  29. if (Screen.dpi < 1) dpiScale = 1;
  30. if (Screen.dpi < 200) dpiScale = 1;
  31. else dpiScale = Screen.dpi/200f;
  32. if (pos.x < 380*dpiScale && Screen.height - pos.y < 250*dpiScale) return;
  33. Cursor.visible = false;
  34. Cursor.lockState = CursorLockMode.Locked;
  35. x += Input.GetAxis("Mouse X")*xSpeed*0.02f;
  36. y -= Input.GetAxis("Mouse Y")*ySpeed*0.02f;
  37. y = ClampAngle(y, yMinLimit, yMaxLimit);
  38. var rotation = Quaternion.Euler(y, x, 0);
  39. var position = rotation* new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
  40. transform.rotation = rotation;
  41. transform.position = position;
  42. }
  43. else
  44. {
  45. Cursor.visible = true;
  46. Cursor.lockState = CursorLockMode.None;
  47. //Cursor.visible = true;
  48. //Screen.lockCursor = false;
  49. }
  50. if(Input.GetKey(KeyCode.RightArrow))
  51. {
  52. if (Time.timeScale < 0.5) x += 1f / 60f * 100;
  53. else x += Time.deltaTime * 3;
  54. var rotation = Quaternion.Euler(y, x, 0);
  55. var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
  56. transform.rotation = rotation;
  57. transform.position = position;
  58. }
  59. if (Input.GetKey(KeyCode.LeftArrow))
  60. {
  61. if(Time.timeScale < 0.5) x -= 1f / 60f * 100;
  62. else x -= Time.deltaTime * 3;
  63. var rotation = Quaternion.Euler(y, x, 0);
  64. var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
  65. transform.rotation = rotation;
  66. transform.position = position;
  67. }
  68. if (Input.GetKeyDown(KeyCode.UpArrow))
  69. {
  70. if (Time.timeScale > 0.5) Time.timeScale = 0;
  71. else Time.timeScale = 1;
  72. }
  73. if (Math.Abs(prevDistance - distance) > 0.001f)
  74. {
  75. prevDistance = distance;
  76. var rot = Quaternion.Euler(y, x, 0);
  77. var po = rot*new Vector3(0.0f, 0.0f, -distance) + target.transform.position;
  78. transform.rotation = rot;
  79. transform.position = po;
  80. }
  81. }
  82. static float ClampAngle(float angle, float min, float max)
  83. {
  84. if (angle < -360)
  85. angle += 360;
  86. if (angle > 360)
  87. angle -= 360;
  88. return Mathf.Clamp(angle, min, max);
  89. }
  90. }