Navigating Unity NavMesh

Hal Brooks
3 min readAug 20, 2021

--

Objective: Allow game view to change enabling player to use point and click to navigate the scene.

Unity AI is used to create a navigable surface that the player can move along. A point and click movement system with NavMesh allows a smooth movement system, as long as the player stays in the field of view. For the player to navigate the entire scene, the main camera needs to change positions depending on the players position. This is done by having a series of box colliders, Camera_16_Trigger, organized under Camera_Progression_Triggers. Each trigger has a game object Camera_One to Six organized under Camera_Progression_Angles.

Hierarchy showing camera positions and triggers.

Camera_1 to 3_Triggers can be seen in the image below as long boxes.

The green lines are the boundaries for the box collider triggers.

In the scene, the is trigger feature is used to change camera positions. Each camera trigger, for example Camera_1_Trigger below, must have a box collider with is trigger checked and the CameraTrigger script components.

The Camera Trigger script is shown below, and requires the assignment of the corresponding _camera transform for each camera trigger. Because _camera is preceded by [SerializeField] the camera_progression_angles can be assigned for each instance in the inspector, as shown above.

CameraTrigger script to change main camera position and rotation.

Add a rigidbody component to the player script, setting use gravity to unchecked and check is kinematic. This is required to trigger the events. Placing the rigidbody on the Player requires a single rigidbody, as opposed to six if they were placed on each camera trigger. If the player enters the trigger then the camera position and rotation is changed to the corresponding camera angle. Finally the main camera should focus on the Player using the LookAtPlayer script below, which is attached to the main camera. This script assigns the _player in void Start allowing the Main Camera to continually update the view to look at the Player using Camera.main.transfrom.LookAt function.

LookAtPlayer script for main camera.

The NavMesh can now be used to move across the full field of play as the view changes based on the Player’s location.

Game view showing main camera looking at the player and changing perspective on hitting Camera_3_Trigger

This article uses assets from The Great Fleece by GameDevJon available on the Unity Asset Store.

--

--

No responses yet