Game Development Reference
In-Depth Information
if(Distance > 10.00f)
{
Destination =
Waypoints[curWaypoint].position;
navAgent.SetDestination(Destination);
break;
}
else if(Distance < 2.00f)
{
ChangeBehavior(Behaviors.Idle);
}
}
}
What this
for
loop does is pick a waypoint that has
Distance
of more than
10
. If it
does, then we set the
Destination
value to the current waypoint and move the AI
accordingly. If the distance from the current waypoint is less than
2
, we change the
behavior to
Idle
.
The next function that we will adjust is the
SearchForTarget
function. Add the fol-
lowing code to it, replacing its previous emptiness:
void SearchForTarget()
{
Destination =
GameObject.FindGameObjectWithTag("Player").transform.position;
navAgent.SetDestination(Destination);
Distance =
Vector3.Distance(gameObject.transform.position,
Destination);
if(Distance < 10)
ChangeBehavior(Behaviors.Combat);
}
This function will now be able to search for a target, the
Player
target to be more
specific. We set
Destination
to the player's current position, and then move the