Using the Unity NavMesh
For anyone that hasn’t ever dabbled with the NavMesh system in Unity, it is an amazing tool that allows you to easily control the boundaries of a player or AI. I won’t get too in-depth about the NavMesh system but you can definitely get pretty deep into its customization. I will post some resources that helped me to understand, learn, and utilize the potential of the Unity NavMesh.
For anyone that wants to get into the nitty-gritty documentation of the NavMesh here is a link to the Unity documentation: https://docs.unity3d.com/2019.4/Documentation/Manual/Navigation.html
If you are not familiar with the NavMesh I definitely suggest at least skimming through the documentation to get a base understanding of its capabilities and setup.
Here is the link to the NavMesh Building components page: https://docs.unity3d.com/2019.4/Documentation/Manual/NavMesh-BuildingComponents.html.
This page will direct you to the NavMesh Components page, which is something you want to import into your project to open up deeper customization of the NavMesh system.
Here is the GitHub link to the NavMesh Components page: https://github.com/Unity-Technologies/NavMeshComponents.
On the GitHub page, feel free to read through the instructions, but basically, all you need to do is navigate to the “NavMeshComponents” folder in “Assets” and download and import “NavMeshComponents” into your project. This will now allow you to customize your NavMesh further with the features in the picture above.
NavMeshSurface Component
The NavMeshSurface component is very useful for setting up NavMesh surfaces for various agent types. Meaning, you can now create multiple NavMesh Agent profiles and control which surfaces they can interact with. Without this component, you only have one NavMesh surface and therefore, cannot customize multiple agents and NavMesh interactions. An example of this would be if you wanted one agent to only be able to traverse “water” and not “land.” And another agent to be able to navigate both surfaces. You can do this by adding multiple NavMeshSurface components for each Agent Type, and setting up which layers you want the agent to be able to interact with.
NavMeshLink Component
The NavMeshLink component allows the NavMesh Agent to travel from one NavMesh Surface to another when there is no direct path. You can customize the link to link two surfaces horizontal or vertical to each other.
NavMesh Modifier Component
The NavMesh Modifier Component can be used to mark a certain area with an area type. In the example above I set the right plane to area type “Jump.” In order to apply the NavMesh Modifier Component, select the object you want to apply it to and add the component. After you set up the Modifier Component, rebake the NavMesh to see the changes.
NavMesh Modifier Volume Component
The NavMesh Modifier Volume Component is good to modify an area/surface that might not be represented as separate geometry. In the example above you can see that it is used to mark an area as non-walkable, but can be used to mark danger zones, walkable zones, etc.
Getting the Agent To Move on the NavMesh
In your script make sure to add “using UnityEngine.AI;” to access the NavMeshAgent component. From there you can use a simple “agent.SetDestination(goal.position);” to move your agent to the target position.
Here is a link to Unity’s documentation for controlling the Agent: https://docs.unity3d.com/Manual/nav-HowTos.html.
You will find different ways to move the Agent, from patrolling between multiple points, moving to a position clicked by the mouse, or using the SetDestination function.
Animating
After you have your NavMesh system customized and set up, it is now time to set up the animations to match your players' movements. One of the best/easiest ways I found to set this up easily is through this ThirdPlayerController script I found in a Brackeys video.
In my script above, I accessed the NavMesh Agent to use the distance functions to set up my animations. In Unity, you can set a stopping distance on your NavMesh Agent and through the script, you can control which animation plays when the player is outside and inside of that threshold. Make sure to add “using UnityEngine.AI” and “using UnityStandardAssets.Characters.ThirdPerson” to access the NavMesh Agent and Third-Person functions.
Here is the link to the repository: https://github.com/Brackeys/NavMesh-Tutorial/blob/master/NavMesh%20Example%20Project/Assets/Example05%20-%20LowMan/LowMan/Scripts/ThirdPersonCharacter.cs.
The script trivializes the need to match the speed of the animation to the character and has very little setup to get working. Add the script to any game object you want to move and animate.
Additional Resources:
Below is a link to a 3 part Brackeys tutorial on youtube
https://www.youtube.com/watch?v=CHV1ymlw-P8&t=3s&ab_channel=Brackeys