Blocker.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEngine;
  2. using System.Collections;
  3. [AddComponentMenu("Cocuy/Blocker")]
  4. public class Blocker : MonoBehaviour {
  5. public FluidSimulator m_fluid;
  6. [HideInInspector]
  7. public bool m_useScaleAsSize = true;
  8. [HideInInspector]
  9. public float m_radius = 0.1f;
  10. [HideInInspector]
  11. public bool m_showGizmo = false;
  12. public float GetRadius()
  13. {
  14. if (m_useScaleAsSize)
  15. {
  16. return Mathf.Max(transform.lossyScale.x, transform.lossyScale.y);
  17. }
  18. return m_radius;
  19. }
  20. void LateUpdate()
  21. {
  22. Ray ray = new Ray(transform.position, new Vector3(0, 0, 1));
  23. RaycastHit hitInfo = new RaycastHit();
  24. if (m_fluid.GetComponent<Collider>().Raycast(ray, out hitInfo, 10))
  25. {
  26. float fWidth = m_fluid.GetComponent<Renderer>().bounds.extents.x * 2f;
  27. //float fHeight = m_fluid.renderer.bounds.extents.z * 2f;
  28. float fRadius = (GetRadius() * m_fluid.GetWidth()) / fWidth;
  29. m_fluid.AddObstacleCircle(hitInfo.textureCoord, fRadius, false);
  30. }
  31. }
  32. void DrawGuidelines()
  33. {
  34. Gizmos.color = Color.cyan;
  35. Gizmos.DrawWireSphere(transform.position, GetRadius());
  36. }
  37. void OnDrawGizmosSelected()
  38. {
  39. DrawGuidelines();
  40. }
  41. void OnDrawGizmos()
  42. {
  43. if (m_showGizmo)
  44. {
  45. DrawGuidelines();
  46. }
  47. }
  48. }