ETFXMouseOrbit.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace EpicToonFX
  4. {
  5. public class ETFXMouseOrbit : MonoBehaviour
  6. {
  7. public Transform target;
  8. public float distance = 12.0f;
  9. public float xSpeed = 120.0f;
  10. public float ySpeed = 120.0f;
  11. public float yMinLimit = -20f;
  12. public float yMaxLimit = 80f;
  13. public float distanceMin = 8f;
  14. public float distanceMax = 15f;
  15. public float smoothTime = 2f;
  16. private float rotationYAxis = 0.0f;
  17. private float rotationXAxis = 0.0f;
  18. private float velocityX = 0.0f;
  19. private float maxVelocityX = 0.1f;
  20. private float velocityY = 0.0f;
  21. private readonly float autoRotationSmoothing = 0.02f;
  22. [HideInInspector] public bool isAutoRotating = false;
  23. [HideInInspector] public ETFXEffectController etfxEffectController;
  24. [HideInInspector] public ETFXEffectControllerPooled etfxEffectControllerPooled;
  25. private void Start()
  26. {
  27. Vector3 angles = transform.eulerAngles;
  28. rotationYAxis = angles.y;
  29. rotationXAxis = angles.x;
  30. // Make the rigid body not change rotation
  31. if (GetComponent<Rigidbody>())
  32. {
  33. GetComponent<Rigidbody>().freezeRotation = true;
  34. }
  35. }
  36. private void Update()
  37. {
  38. if(target)
  39. {
  40. if (Input.GetMouseButton(1))
  41. {
  42. velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
  43. velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
  44. if (isAutoRotating)
  45. {
  46. StopAutoRotation();
  47. }
  48. }
  49. distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 15, distanceMin, distanceMax);
  50. }
  51. }
  52. private void FixedUpdate()
  53. {
  54. if (target)
  55. {
  56. rotationYAxis += velocityX;
  57. rotationXAxis -= velocityY;
  58. rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
  59. Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
  60. Quaternion rotation = toRotation;
  61. if (Physics.Linecast(target.position, transform.position, out RaycastHit hit))
  62. {
  63. distance -= hit.distance;
  64. }
  65. Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
  66. Vector3 position = Vector3.Lerp(transform.position, rotation * negDistance + target.position, 0.6f);
  67. transform.rotation = rotation;
  68. transform.position = position;
  69. velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
  70. velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
  71. }
  72. }
  73. public static float ClampAngle(float angle, float min, float max)
  74. {
  75. if (angle < -360F)
  76. angle += 360F;
  77. if (angle > 360F)
  78. angle -= 360F;
  79. return Mathf.Clamp(angle, min, max);
  80. }
  81. public void InitializeAutoRotation()
  82. {
  83. isAutoRotating = true;
  84. StartCoroutine(AutoRotate());
  85. }
  86. public void SetAutoRotationSpeed(float rotationSpeed)
  87. {
  88. maxVelocityX = rotationSpeed;
  89. }
  90. private void StopAutoRotation()
  91. {
  92. if (etfxEffectController != null)
  93. etfxEffectController.autoRotation = false;
  94. if (etfxEffectControllerPooled != null)
  95. etfxEffectControllerPooled.autoRotation = false;
  96. isAutoRotating = false;
  97. StopAllCoroutines();
  98. }
  99. IEnumerator AutoRotate()
  100. {
  101. int lerpSteps = 0;
  102. while (lerpSteps < 30)
  103. {
  104. velocityX = Mathf.Lerp(velocityX, maxVelocityX, autoRotationSmoothing);
  105. yield return new WaitForFixedUpdate();
  106. }
  107. while (isAutoRotating)
  108. {
  109. velocityX = maxVelocityX;
  110. yield return new WaitForFixedUpdate();
  111. }
  112. }
  113. }
  114. }