LocalSpaceRichAI.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using UnityEngine;
  2. namespace Pathfinding.Examples {
  3. /// <summary>
  4. /// RichAI for local space (pathfinding on moving graphs).
  5. ///
  6. /// What this script does is that it fakes graph movement.
  7. /// It can be seen in the example scene called 'Moving' where
  8. /// a character is pathfinding on top of a moving ship.
  9. /// The graph does not actually move in that example
  10. /// instead there is some 'cheating' going on.
  11. ///
  12. /// When requesting a path, we first transform
  13. /// the start and end positions of the path request
  14. /// into local space for the object we are moving on
  15. /// (e.g the ship in the example scene), then when we get the
  16. /// path back, they will still be in these local coordinates.
  17. /// When following the path, we will every frame transform
  18. /// the coordinates of the waypoints in the path to global
  19. /// coordinates so that we can follow them.
  20. ///
  21. /// At the start of the game (when the graph is scanned) the
  22. /// object we are moving on should be at a valid position on the graph and
  23. /// you should attach the <see cref="Pathfinding.LocalSpaceGraph"/> component to it. The <see cref="Pathfinding.LocalSpaceGraph"/>
  24. /// component will store the position and orientation of the object right there are the start
  25. /// and then we can use that information to transform coordinates back to that region of the graph
  26. /// as if the object had not moved at all.
  27. ///
  28. /// This functionality is only implemented for the RichAI
  29. /// script, however it should not be hard to
  30. /// use the same approach for other movement scripts.
  31. /// </summary>
  32. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_local_space_rich_a_i.php")]
  33. public class LocalSpaceRichAI : RichAI {
  34. /// <summary>Root of the object we are moving on</summary>
  35. public LocalSpaceGraph graph;
  36. void RefreshTransform () {
  37. graph.Refresh();
  38. richPath.transform = graph.transformation;
  39. movementPlane = graph.transformation;
  40. }
  41. protected override void Start () {
  42. RefreshTransform();
  43. base.Start();
  44. }
  45. protected override void CalculatePathRequestEndpoints (out Vector3 start, out Vector3 end) {
  46. RefreshTransform();
  47. base.CalculatePathRequestEndpoints(out start, out end);
  48. start = graph.transformation.InverseTransform(start);
  49. end = graph.transformation.InverseTransform(end);
  50. }
  51. protected override void Update () {
  52. RefreshTransform();
  53. base.Update();
  54. }
  55. }
  56. }