FluidFollower.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using UnityEngine;
  2. using System.Collections;
  3. [AddComponentMenu("Cocuy/Fluid Follower")]
  4. public class FluidFollower : MonoBehaviour {
  5. public FluidSimulator m_fluid;
  6. public ParticlesArea m_particleArea;
  7. void Start()
  8. {
  9. if (m_fluid && !m_fluid.m_cacheVelocity)
  10. {
  11. Debug.LogWarning("<CocuyWarning> \"Cache Velocity\" must be set to true on the FluidSumulator component to use the Fluid Follower.");
  12. }
  13. }
  14. void LateUpdate()
  15. {
  16. if (m_fluid)
  17. {
  18. Ray ray = new Ray(gameObject.transform.position, new Vector3(0, 0, 1));
  19. RaycastHit hitInfo = new RaycastHit();
  20. if (m_fluid.GetComponent<Collider>().Raycast(ray, out hitInfo, 10))
  21. {
  22. Vector2 simSize = new Vector2(m_fluid.GetWidth(), m_fluid.GetHeight());
  23. Vector2 posInSimSpace = new Vector2(hitInfo.textureCoord.x * simSize.x, hitInfo.textureCoord.y * simSize.y);
  24. Vector2 velInSimSpace = m_fluid.GetVelocity((int)posInSimSpace.x, (int)posInSimSpace.y) * Time.deltaTime;
  25. Vector2 worldSize = m_particleArea.GetRenderSize();
  26. Vector2 velInWorldSpace = new Vector2((velInSimSpace.x * worldSize.x) / simSize.x, (velInSimSpace.y * worldSize.y) / simSize.y);
  27. gameObject.transform.position = gameObject.transform.position + new Vector3(velInWorldSpace.x, velInWorldSpace.y, 0f);
  28. }
  29. }
  30. }
  31. }