Sprinkler.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.U2D;
  5. public class Sprinkler : MonoBehaviour
  6. {
  7. public GameObject m_Prefab;
  8. public float m_RandomFactor = 10.0f;
  9. public bool m_UseNormals = false;
  10. float Angle(Vector3 a, Vector3 b)
  11. {
  12. float dot = Vector3.Dot(a, b);
  13. float det = (a.x * b.y) - (b.x * a.y);
  14. return Mathf.Atan2(det, dot) * Mathf.Rad2Deg;
  15. }
  16. // Use this for initialization. Plant the Prefabs on Startup
  17. void Start ()
  18. {
  19. SpriteShapeController ssc = GetComponent<SpriteShapeController>();
  20. Spline spl = ssc.spline;
  21. for (int i = 1; i < spl.GetPointCount() - 1; ++i)
  22. {
  23. if (Random.Range(0, 100) > (100 - m_RandomFactor) )
  24. {
  25. var go = GameObject.Instantiate(m_Prefab);
  26. go.transform.position = spl.GetPosition(i);
  27. if (m_UseNormals)
  28. {
  29. Vector3 lt = Vector3.Normalize(spl.GetPosition(i - 1) - spl.GetPosition(i));
  30. Vector3 rt = Vector3.Normalize(spl.GetPosition(i + 1) - spl.GetPosition(i));
  31. float a = Angle(Vector3.up, lt);
  32. float b = Angle(lt, rt);
  33. float c = a + (b * 0.5f);
  34. if (b > 0)
  35. c = (180 + c);
  36. go.transform.rotation = Quaternion.Euler(0, 0, c);
  37. }
  38. }
  39. }
  40. }
  41. // Update is called once per frame
  42. void Update ()
  43. {
  44. }
  45. }