Implementing some behaviors using the NavMesh API
In Chapter 1, Behaviors – Intelligent Movement, we learned about different types of movement for our agents. We can implement some of them in a different way using the NavMesh API and the NavMeshAgent
component. Thus, we'll be able to prototype similar behaviors in a faster, cleaner, and more Unity-ish way.
We will learn how to implement the patrolling behavior so that we can have a different angle and get ideas on how to fast-prototype different behaviors beyond the basic flow.
Getting ready
It is important to have grasped the basics of NavMesh and baking and handling the NavMeshAgent
component before working on this recipe.
How to do it...
We will create a patrol behavior component to be used along with a NavMeshAgent
component:
- Create a script named
NMPatrol.cs
, and include theNavMeshAgent
as a required component:
using UnityEngine; using UnityEngine.AI; [RequireComponent(typeof(NavMeshAgent))] public class NMPatrol : MonoBehaviour { ...