We will create a patrol behavior component to be used along with a NavMeshAgent component:
- Create a script named NMPatrol.cs, and include the NavMeshAgent as a required component:
using UnityEngine;using UnityEngine.AI;[RequireComponent(typeof(NavMeshAgent))]public class NMPatrol : MonoBehaviour{ // next steps}
- Add the member variables required:
public float pointDistance = 0.5f;public Transform[] patrolPoints;private int currentPoint = 0;private NavMeshAgent agent;
- Create a function for finding the closest patrol point in the array:
private int FindClosestPoint(){ // next step}
- Add the internal variables required:
int index = -1;float distance = Mathf.Infinity;int i;Vector3 agentPosition = transform.position;Vector3 ...