Game Development Reference
In-Depth Information
AI towards the player. When
Distance
is less than
10
, we set the AI behavior to
Combat
.
Now that our AI can run from the player as well as chase them down, let's utilize the
waypoints and create paths for the AI. Add this code to the empty
Patrol
function:
void Patrol()
{
Distance =
Vector3.Distance(gameObject.transform.position,
Waypoints[curWaypoint].position);
if(Distance > 2.00f)
{
Destination =
Waypoints[curWaypoint].position;
navAgent.SetDestination(Destination);
}
else
{
if(ReversePath)
{
if(curWaypoint <= 0)
{
ReversePath = false;
}
else
{
curWaypoint--;
Destination =
Waypoints[curWaypoint].position;
}
}
else
{
if(curWaypoint >= Waypoints.Length - 1)
{
ReversePath = true;
}