Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. namespace Pathfinding.Examples {
  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 #Pathfinding.LocalSpaceGraph component to it. The #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.      *
  32.      * \astarpro
  33.      */
  34.     public class LocalSpaceRichAI : RichAI {
  35.         /** Root of the object we are moving on */
  36.         public LocalSpaceGraph graph;
  37.  
  38.         void RefreshTransform () {
  39.             graph.Refresh();
  40.             richPath.transform = graph.transformation;
  41.             movementPlane = graph.transformation;
  42.         }
  43.  
  44.         protected override void Start () {
  45.             RefreshTransform();
  46.             base.Start();
  47.         }
  48.  
  49.         protected override void CalculatePathRequestEndpoints (out Vector3 start, out Vector3 end) {
  50.             RefreshTransform();
  51.             base.CalculatePathRequestEndpoints(out start, out end);
  52.             start = graph.transformation.InverseTransform(start);
  53.             end = graph.transformation.InverseTransform(end);
  54.         }
  55.  
  56.         protected override void Update () {
  57.             RefreshTransform();
  58.             base.Update();
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement