In this lecture, we will start looking at one of the most fundamental AI techniques used in navigation
waypoints.
Waypoints are simply positions on a map that, when navigated in sequence, produce a path.
It's the same system used in a car GPS that gives you instructions on where to drive.
Waypoints are an easy system to implement in an NPC as it makes them look more intelligent than they
are.
And by providing them with a safe set of locations on a map to move between, they can also avoid obstacles.
We've already created an embassy that can move and follow a goal location.
Updating this to a waypoint system is straightforward.
In this video, we're going to put together a waypoint system and have these tanks drive around from
one to the other in a circuit.
To begin with, you need the starter resource that's attached to this lecture.
So download that open up a new unity project and bring those resources in.
Inside of it.
You're going to find a scene called Tank Racing, and if you open up that scene, you'll get something
like this.
So we have a ground which is made of a cube that is stretched out with a texture on it.
There's four different colored tanks and there's a palm tree, the palm tree we will use for our waypoints.
Essentially, a waypoint only needs to be a 3D position or 2D.
If you're on a 2D map, that's somewhere in your game environment.
Usually when I'm putting waypoints in for, say, a racing track or inside of a game map, then I just
use the sphere or a cube in unity just so I can see where it is.
And then later on you can remove its renderer and make sure, of course, if you've got waypoints,
that you do take off any colliders because they're not really in the environment.
They're just showing us the positions where our AI controlled characters should go between.
But in this case, I'm using a palm tree so we can actually see it.
And in this kind of desert scene, it kind of makes sense.
I guess with your palm tree, what you need to do is to control DX to duplicate it, then hit your w
key and possibly in a top down view.
Just move that palm tree, try and keep it on the plane, but just move it.
And what we're going to do is just make a bit of a circuit with it.
Control D again, grab the duplicate and then Control D.
Make yourself.
Just a bit of a circular.
Thing that your tank can go around.
Okay.
So this tank is going to go around these and use it kind of like a little bit of a racing track.
And these are the flag points for it.
All right.
So now let's actually write some code to achieve this.
So go create C-sharp down in your assets.
And we're going to call this follow waypoint.
So follow woop and then open that up.
First of all, your script needs to know about the waypoints themselves and we're going to put those
into an array of game objects.
So create a public game object array for your waypoints.
Then we also need an integer to keep track of which waypoint we're on.
So we'll call that current waypoint and we will set that to a value of zero.
Because your array index always starts at zero.
So that very first tree will be the starting point.
Okay, now we have to move our tank around and therefore, while we're up here, let's just create a
public float for speed.
And we will set that to 10.0 F and of course, you can change that however you like.
All right.
We don't need a start at this point, but down in update, we're going to basically loop through all
these waypoints.
But we don't want to go to the next waypoint until we've actually reached the next waypoint.
Does that make sense?
Okay.
So we start at Waypoint zero and then we test if we're close enough to that waypoint and then we go
to the next waypoint.
Now, do you remember how we test to find out if our character is actually near its goal location?
Because this is what we need to do for each waypoint.
So we have to use a distance.
And in here I'm going to put if vector three.
Dot distance, so we'll use Unity's Maths Library rather than writing our own again.
And we're going to find the distance between this dot transform dot position.
So that's going to be the tank and the waypoints.
Current white point.
So this is the first waypoint when we first come in here.
Transform position.
And then we need to say, well, when are we close enough to that particular waypoint?
So let's say if it's less than three, we will consider that we've actually reached that waypoint.
And when we do, we're going to update what current waypoint is, current waypoint plus plus.
So then current waypoint will be one and therefore we can start heading towards that.
Now at some point our waypoints are going to run out.
So say we've got ten waypoints.
At some point, current waypoint is going to be an index value of ten or greater, which won't exist
inside of this array.
Because if you've got ten waypoints, then you only have indexes of 0 to 9.
Now when that happens, we need to actually switch our current waypoint back to the first one.
So if current waypoint is greater than or equal to our waypoints length.
Then we're going to go current waypoint
equals zero and we're also going to spell it correctly.
Okay, so there's our current waypoint.
So now we've taken care of going to the next waypoint and also going back to the zero waypoint if we
reached the end.
The next thing we really need to be doing is actually pushing our tank forward.
So we're going to grow this dot, transform dot, look at and remember what it does.
It's going to turn your character to look at another location.
So in this case, we want to look at the next waypoint or look at the waypoint we're going towards.
So waypoints current waypoint dot transform
and then this dot transform dot trends late zero zero.
So nothing in the X and the Y directions, but we want to go along the z at speed multiplied by time.
Dot Delta time because we're inside of the update.
All right.
So save that.
Go back into unity and we need to put this onto a tank.
So we're just going to start with one tank.
So select your green tank or whichever one you want.
Drag and drop that follow waypoint script onto there.
Okay.
We now have our waypoints array at the top here that we can put all of these palm trees in.
Now you can put them in one by one, or you can select the whole lot of them.
If you created them in a circuit in the order of your circuit, then it's going to be much easier just
to drag and drop the whole lot in and then they'll end up in the right order in the waypoints array.
So just go up to the top and click on the little lock which will lock this inspector view.
Then over in the hierarchy you can select the first palm tree shift, select the last palm tree, and
just drag and drop those on to waypoints, and that will populate that whole array in the correct order.
Remember, we're going from one waypoint to the other.
So if you change these ordering around, that will be the way that the tank will go.
Okay, so let's just remember to unlock our inspector view and now we're ready to try this out.
So press play and we will see that our tank goes off around the waypoints.
Okay.
So with our current setup, we've got our look at which is making our tank snap around to look at the
new waypoints.
Now, that's quite an unnatural looking way for a character to move.
We actually want it to sort of turn more slowly and more naturally towards the next waypoint.
So in the next video, we'll come back and deal with that.