Recast graph on a moving surface

Example scene which demonstrates navigation on a moving surface.

This movement scene is a bit different. The agent moves around on a spaceship that is simultaneously moving around in space.


Graph setup

Moving every single node in the graph every frame would be slow, and propagating this information to already calculated paths would also be pretty tricky. So instead, the movement script that we use here ( LocalSpaceRichAI) cheats.

The graph in the scene is actually static. It stays in the same position as the ship moves around. However, when the graph is scanned as the game starts, the position of the ship is recorded. When it is time to calculate a path, the movement script will transform its position and destination so that it looks as if the ship was still at its initial position. This means it will match the scanned graph perfectly. Similarly, when it follows the path, it will transform all points on the path to account for the current position and rotation of the ship.

This has some limitations, for example navmesh cutting and other graph updates will not work. But following a path works just fine regardless of how the ship moves.

Detailed setup

The scene is configured like this:

  1. The ship is added. This can be any mesh or collection of meshes.

  2. A recast graph is added, and configured to cover the ship at its initial position.

  3. An agent is added as a child of the ship GameObject. This ensures that it will move with the ship.

  4. The LocalSpaceGraph component is added to the ship GameObject. This will track the initial position of the ship, and provide the means for the agent to translate back and forth between the ship's coordinate system and the world coordinate system.

  5. The LocalSpaceRichAI component is added to the agent, and the LocalSpaceGraph component is assigned to its LocalSpaceRichAI.graph field.

  6. The ship is made to move using the BezierMover component. This component moves the ship through a series of points, along a smooth curve.