Game Development Reference
In-Depth Information
Adding our variables
To start our navigation system, we will need to add a few variables first. Place these
with the rest of our variables:
public Transform[] Waypoints;
public int curWaypoint = 0;
bool ReversePath = false;
NavMeshAgent navAgent;
Vector3 Destination;
float Distance;
The first variable is an array of Transforms ; this is what we will use to hold our
waypoints. Next, we have an integer that is used to iterate through our Transform
array. We have a bool variable, which will decide how we should navigate through
the waypoints.
The next three variables are more oriented towards our navigation mesh that we cre-
ated earlier. The NavMeshAgent object is what we will reference when we want to
interact with the navigation mesh. The destination will be the location that we want
the AI to move towards. The distance is what we will use to check how far away we
are from that location.
Scripting the navigation functions
Previously, we created many empty functions; some of these are dependent on
pathfinding. Let's start with the Flee function. Add this code to replace the empty
function:
void Flee()
{
for(int fleePoint = 0; fleePoint <
Waypoints.Length; fleePoint++)
{
Distance =
Vector3.Distance(gameObject.transform.position,
Waypoints[fleePoint].position);
Search WWH ::




Custom Search