RVOSquareObstacle.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using UnityEngine;
  2. namespace Pathfinding.RVO {
  3. /// <summary>Square Obstacle for RVO Simulation.</summary>
  4. [AddComponentMenu("Pathfinding/Local Avoidance/Square Obstacle")]
  5. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_r_v_o_1_1_r_v_o_square_obstacle.php")]
  6. public class RVOSquareObstacle : RVOObstacle {
  7. /// <summary>Height of the obstacle</summary>
  8. public float height = 1;
  9. /// <summary>Size of the square</summary>
  10. public Vector2 size = Vector3.one;
  11. /// <summary>Center of the square</summary>
  12. public Vector2 center = Vector3.zero;
  13. protected override bool StaticObstacle { get { return false; } }
  14. protected override bool ExecuteInEditor { get { return true; } }
  15. protected override bool LocalCoordinates { get { return true; } }
  16. protected override float Height { get { return height; } }
  17. //If UNITY_EDITOR to save a few bytes, these are only needed in the editor
  18. #if UNITY_EDITOR
  19. private Vector2 _size;
  20. private Vector2 _center;
  21. private float _height;
  22. #endif
  23. protected override bool AreGizmosDirty () {
  24. #if UNITY_EDITOR
  25. bool ret = _size != size || _height != height || _center != center;
  26. _size = size;
  27. _center = center;
  28. _height = height;
  29. return ret;
  30. #else
  31. return false;
  32. #endif
  33. }
  34. protected override void CreateObstacles () {
  35. size.x = Mathf.Abs(size.x);
  36. size.y = Mathf.Abs(size.y);
  37. height = Mathf.Abs(height);
  38. var verts = new [] { new Vector3(1, 0, -1), new Vector3(1, 0, 1), new Vector3(-1, 0, 1), new Vector3(-1, 0, -1) };
  39. for (int i = 0; i < verts.Length; i++) {
  40. verts[i].Scale(new Vector3(size.x * 0.5f, 0, size.y * 0.5f));
  41. verts[i] += new Vector3(center.x, 0, center.y);
  42. }
  43. AddObstacle(verts, height);
  44. }
  45. }
  46. }