MegaFlowCameraOrbit.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. 
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. public class MegaFlowCameraOrbit : MonoBehaviour
  5. {
  6. public GameObject target;
  7. MeshRenderer render;
  8. SkinnedMeshRenderer srender;
  9. MeshFilter filter;
  10. public float distance = 10.0f;
  11. public float xSpeed = 250.0f;
  12. public float ySpeed = 120.0f;
  13. public float zSpeed = 120.0f;
  14. public float yMinLimit = -20.0f;
  15. public float yMaxLimit = 80.0f;
  16. public float xMinLimit = -20.0f;
  17. public float xMaxLimit = 20.0f;
  18. public float x = 0.0f;
  19. public float y = 0.0f;
  20. private Vector3 center;
  21. public bool Dynamic = false;
  22. public Vector3 offset;
  23. public Vector3 EditTest;
  24. Vector3 tpos = new Vector3(); // target pos
  25. public bool hitfloor = false;
  26. void Start()
  27. {
  28. NewTarget(target);
  29. if ( target )
  30. {
  31. tpos = target.transform.position;
  32. Vector3 angles = Quaternion.LookRotation(tpos - transform.position).eulerAngles;
  33. x = angles.y;
  34. y = angles.x;
  35. distance = (tpos - transform.position).magnitude;
  36. }
  37. else
  38. {
  39. Vector3 angles = transform.eulerAngles;
  40. x = angles.y;
  41. y = angles.x;
  42. }
  43. nx = x;
  44. ny = y;
  45. nz = distance;
  46. vx = 0.0f;
  47. vy = 0.0f;
  48. // Make the rigid body not change rotation
  49. //if ( rigidbody )
  50. //rigidbody.freezeRotation = true;
  51. }
  52. private float easeInOutQuint(float start, float end, float value)
  53. {
  54. value /= .5f;
  55. end -= start;
  56. if ( value < 1 ) return end / 2 * value * value * value * value * value + start;
  57. value -= 2;
  58. return end / 2 * (value * value * value * value * value + 2) + start;
  59. }
  60. float easeInQuad(float start, float end, float value)
  61. {
  62. value /= 1.0f;
  63. end -= start;
  64. return end * value * value + start;
  65. }
  66. float easeInSine(float start, float end, float value)
  67. {
  68. end -= start;
  69. return -end * Mathf.Cos(value / 1.0f * (Mathf.PI / 2.0f)) + end + start;
  70. }
  71. float t = 0.0f;
  72. public void NewTarget(GameObject targ)
  73. {
  74. if ( target != targ )
  75. {
  76. target = targ;
  77. t = 0.0f;
  78. if ( target )
  79. {
  80. filter = (MeshFilter)target.GetComponent(typeof(MeshFilter));
  81. if ( filter != null )
  82. {
  83. //center = filter.mesh.bounds.center;
  84. center = filter.sharedMesh.bounds.center;
  85. }
  86. else
  87. {
  88. render = (MeshRenderer)target.GetComponent(typeof(MeshRenderer));
  89. if ( render != null )
  90. center = render.bounds.center;
  91. else
  92. {
  93. srender = (SkinnedMeshRenderer)target.GetComponent(typeof(SkinnedMeshRenderer));
  94. if ( srender != null )
  95. center = srender.bounds.center;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. public float trantime = 4.0f;
  102. public float vx = 0.0f;
  103. public float vy = 0.0f;
  104. public float vz = 0.0f;
  105. public float nx = 0.0f;
  106. public float ny = 0.0f;
  107. public float nz = 0.0f;
  108. public float delay = 0.2f;
  109. public float delayz = 0.2f;
  110. public float mindist = 1.0f;
  111. void LateUpdate()
  112. {
  113. if ( target )
  114. {
  115. if ( Input.GetMouseButton(1) )
  116. {
  117. nx = x + Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  118. ny = y - Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  119. }
  120. if ( !Application.isPlaying )
  121. {
  122. x = nx;
  123. y = ny;
  124. }
  125. else
  126. {
  127. x = Mathf.SmoothDamp(x, nx, ref vx, delay); //0.21f); //, 100.0f, Time.deltaTime);
  128. y = Mathf.SmoothDamp(y, ny, ref vy, delay); //0.21f); //, 100.0f, Time.deltaTime);
  129. }
  130. // NOTE: If you get an exception for this line it means you dont have a scroll wheel input setup in
  131. // the input manager, go to Edit/Project Settings/Input and set the Mouse ScrollWheel setting to use 3rd mouse axis
  132. nz = nz - (Input.GetAxis("Mouse ScrollWheel") * zSpeed);
  133. //y = ClampAngle(y, yMinLimit, yMaxLimit);
  134. //x = ClampAngle(x, xMinLimit, xMaxLimit);
  135. if ( !Application.isPlaying )
  136. {
  137. distance = nz;
  138. }
  139. else
  140. distance = Mathf.SmoothDamp(distance, nz, ref vz, delayz);
  141. //Vector3 crot = transform.localRotation.eulerAngles;
  142. if ( distance < mindist )
  143. {
  144. distance = mindist;
  145. nz = mindist;
  146. }
  147. Vector3 c;
  148. if ( Dynamic )
  149. {
  150. if ( filter != null )
  151. {
  152. c = target.transform.TransformPoint(filter.mesh.bounds.center + offset);
  153. }
  154. else
  155. {
  156. if ( render != null )
  157. c = target.transform.TransformPoint(render.bounds.center + offset);
  158. else
  159. {
  160. if ( srender != null )
  161. c = target.transform.TransformPoint(srender.bounds.center + offset);
  162. else
  163. c = target.transform.TransformPoint(center + offset);
  164. }
  165. }
  166. }
  167. else
  168. c = target.transform.TransformPoint(center + offset);
  169. // Want to ease to new target pos
  170. if ( t < trantime )
  171. {
  172. t = trantime; //+= Time.deltaTime;
  173. tpos.x = easeInSine(tpos.x, c.x, t / trantime);
  174. tpos.y = easeInSine(tpos.y, c.y, t / trantime);
  175. tpos.z = easeInSine(tpos.z, c.z, t / trantime);
  176. }
  177. else
  178. tpos = c;
  179. Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
  180. Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + tpos; //c;
  181. if ( hitfloor )
  182. {
  183. if ( position.y < 18.0f )
  184. position.y = 18.0f;
  185. }
  186. transform.rotation = rotation;
  187. transform.position = position;
  188. }
  189. }
  190. static float ClampAngle(float angle, float min, float max)
  191. {
  192. if ( angle < -360.0f )
  193. angle += 360.0f;
  194. if ( angle > 360.0f )
  195. angle -= 360.0f;
  196. return Mathf.Clamp(angle, min, max);
  197. }
  198. }