Recast graph with doors

Example scene which demonstrates doors and moving bridges in a 3D level.

This example shows how to use the NavmeshCut component to make doors that can be opened and closed. It also shows how to make a moving bridge that updates the navmesh using the DynamicGridObstacle component.

Contents

Graph setup

This example uses a recast graph because it supports the navmesh cuts that are used for the doors. A grid graph could also have been used, but then the doors would have to be implemented using DynamicGridObstacle components instead, or using some other method.

The ground is pretty much flat, so the recast graph does not require many tweaks from the default settings. The most notable changes are:

  • The cell size of the graph has been reduced a bit to make it represent the world more accurately.

  • The character radius has also been changed to reflect the size of the agents that will be used in the scene.

  • The bounding box of the graph has been changed to fit the world.

  • The layer mask has been changed to only include the Default layer. This is because all obstacles in this scene are on the Default layer.

  • The graph has been changed to rasterize colliders, instead of meshes, which is the default. This is primarily because the bridge is always visible, but the animation on it enables and disables a collider to toggle its traversability.

Doors

There are two doors in the scene with identical configuration. If you click on the red dot in the scene, the agent will move there and the door will open.

The interaction is done via the Interactable component.

Note

The Interactable component is a very simple state machine implementation which is intended for the example scenes. In a real game, you would probably want to use a more advanced state machine implementation, but this script can serve as a good example if you want to write your own state machine code for your game.

It is configured to do the following when clicked on:

  1. Spawn the same "move here" particle effect as when the player normally clicks somewhere on the ground.

  2. Make the agent move to the red dot, and rotate to face the console.

  3. Spawn a bunch of particle effects.

  4. Set the parameter "open" on the door's animator component to true. This will cause the door opening animation to play.

The door's animator can be in either the open state, or the closed state. If the animator detects a change in the "open" parameter, it will play the appropriate transition animation and move to the other state. There is also a NavmeshCut covering the opening. When the opening animation starts, this navmesh cut will be disabled, which allows the agent to move through the door.

Note

You could alternatively use a collider together with a DynamicGridObstacle component, to trigger a recalculation of the graph around the door. This approach is used in the Bridge section.

Bridge

The movable bridge is initially not traversable, but it can be moved into position by moving the agent to the console with the red dot next to it.

It is configured in a very similar way to the doors. The primary difference lies in the animation. The bridge animation will enable or disable a collider object when the animation finished, and this collider object also has a DynamicGridObstacle component attached to it. Enabling or disabling the collider will make the DynamicGridObstacle component recalculate the graph around the bridge from scratch. This is a much more powerful operation than navmesh cutting, as this can change any aspect of the graph, not just remove parts of it. However, it may be slower, depending on your graph complexity.

Note

The DynamicGridObstacle component was named that way when it only supported grid graphs. It can now be used with recast graphs too, so the name is a bit misleading. The name may change in a future update.

Chest

There's a chest in the scene which can be opened by clicking on it. Chests are described in more detail in Chests.