VelocityManipulator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using UnityEngine;
  2. using System.Collections;
  3. [AddComponentMenu("Cocuy/Velocity Manipulator")]
  4. [ExecuteInEditMode]
  5. public class VelocityManipulator : MonoBehaviour
  6. {
  7. public FluidSimulator m_fluid;
  8. [HideInInspector]
  9. public bool m_useScaleAsSize = true;
  10. [HideInInspector]
  11. public bool m_velocityFromMovement = false;
  12. [HideInInspector]
  13. public float m_fluidVelocitySpeed = 1f;
  14. [HideInInspector]
  15. public float m_scaleVelocity = 1f;
  16. [HideInInspector]
  17. public float m_radius = 0.1f;
  18. [HideInInspector]
  19. public bool m_showGizmo = false;
  20. Vector3 m_direction;
  21. Vector3 m_speed;
  22. Vector3 m_prevPosition;
  23. void Start()
  24. {
  25. m_prevPosition = transform.position;
  26. m_direction = GetDirection();
  27. }
  28. public float GetRadius()
  29. {
  30. if (m_useScaleAsSize)
  31. {
  32. return Mathf.Max(transform.lossyScale.x, transform.lossyScale.y);
  33. }
  34. return m_radius;
  35. }
  36. public Vector3 GetDirection()
  37. {
  38. if (m_velocityFromMovement)
  39. {
  40. return transform.position - m_prevPosition;
  41. }
  42. return transform.rotation * Vector3.down;
  43. }
  44. void UpdateValues()
  45. {
  46. m_direction = GetDirection();
  47. if (m_direction != Vector3.zero)
  48. {
  49. m_direction.Normalize();
  50. m_speed = m_direction * m_fluidVelocitySpeed * Time.deltaTime;
  51. }
  52. else
  53. {
  54. m_speed = Vector3.zero;
  55. }
  56. m_prevPosition = transform.position;
  57. }
  58. void Update()
  59. {
  60. UpdateValues();
  61. }
  62. void LateUpdate()
  63. {
  64. if (m_fluid)
  65. {
  66. Vector3 currentPosition = transform.position;
  67. if (m_speed != Vector3.zero)
  68. {
  69. Ray ray = new Ray(currentPosition, new Vector3(0, 0, 1));
  70. RaycastHit hitInfo = new RaycastHit();
  71. if (m_fluid.GetComponent<Collider>().Raycast(ray, out hitInfo, 10))
  72. {
  73. float fWidth = m_fluid.GetComponent<Renderer>().bounds.extents.x * 2f;
  74. //float fHeight = m_fluid.renderer.bounds.extents.z * 2f;
  75. float fRadius = (GetRadius() * m_fluid.GetWidth()) / fWidth;
  76. m_fluid.AddVelocity(hitInfo.textureCoord, -m_speed, fRadius);
  77. }
  78. }
  79. }
  80. }
  81. void DrawGizmo()
  82. {
  83. Gizmos.color = Color.cyan;
  84. Gizmos.DrawWireSphere(transform.position, GetRadius());
  85. if (!m_velocityFromMovement || Application.isPlaying)
  86. {
  87. Vector3 end_pos = transform.position - (m_direction*(2f + (m_fluidVelocitySpeed/500f)*5f));
  88. Gizmos.color = Color.cyan;
  89. Gizmos.DrawLine(transform.position, end_pos);
  90. Vector3 back_dir = (transform.position - end_pos);
  91. back_dir.Normalize();
  92. float angle = 25 * Mathf.Deg2Rad;
  93. float cos = Mathf.Cos(angle);
  94. float sin = Mathf.Sin(angle);
  95. Vector3 arrow = new Vector3(back_dir.x * cos - back_dir.y * sin,
  96. back_dir.x * sin + back_dir.y * cos, 0f);
  97. Gizmos.DrawLine(end_pos, end_pos + arrow * 0.5f);
  98. cos = Mathf.Cos(-angle);
  99. sin = Mathf.Sin(-angle);
  100. arrow = new Vector3(back_dir.x * cos - back_dir.y * sin,
  101. back_dir.x * sin + back_dir.y * cos, 0f);
  102. Gizmos.DrawLine(end_pos, end_pos + arrow * 0.5f);
  103. }
  104. }
  105. void OnDrawGizmosSelected()
  106. {
  107. DrawGizmo();
  108. }
  109. void OnDrawGizmos()
  110. {
  111. if (m_showGizmo)
  112. {
  113. DrawGizmo();
  114. }
  115. }
  116. }