The previous lecture, we finished setting up our graph and our algorithm.
Now we're going to put our waypoints and the edges for those waypoints into our inspector.
First of all, we need to go back to our map that we created and take a look at where all the edges
are between the waypoints.
Now, because I copy and paste in my waypoints instead of dragging the prefab onto the map each time
I had to rename them in the hierarchy manually, which means they're not in any particular logical order.
But that's okay, just so that we know where they are.
And so what I've done here is just listed out what those edges are between each of the waypoints.
Now you'll see that I've got them as a multi-directional waypoint so that you can go both ways, which
means you don't have to sort of list them twice with, for example, 1 to 8 and then 8 to 1.
Because remember in the code we put it.
So if it's multidirectional or bi directional, then it will actually put it in there twice and reverse
those nodes around.
Okay, so I'll show you how to get started putting these in because your waypoints might be slightly
different to mine, so you'll have them in a different order.
But you want to go to your waypoint manager and find where you've got your waypoints and your links
exposed in the inspector.
Now make sure that you click on the padlock to lock that so that we can copy these across.
The waypoints are the easy bit.
You just shift, select all of those and drag them on to waypoints and then drop them there and this
will just automatically populate them for you.
Okay.
The order that they're in here actually doesn't matter whatsoever because these numbers, as I said,
they're just for us so that we know where things are.
Now, as for the links, these might be different to mine.
What you've got in yours.
Let me go through the process of adding just one link.
So if we hit the plus key, we'll have this element zero.
Drop that down.
You'll see that there's a node one and a node two.
Now, if you remember, this is the little structure that we created in our code.
So let's complete this for one thing.
So it's waypoint one to Waypoint eight is that first link that I've got right over near the helipad,
which is this one here, and it's going to go in both directions.
Okay, so it goes that way.
Then we hit plus again and then we go and put this element in.
Now it's going to copy it to be exactly the same as this one.
That's alright.
You're just going to empty them out anyway.
Okay.
I'm going to go through the process now of putting all of these in and you can do yours too.
So I'll just speed up the video to get us past this.
All right, that's done.
Now we can program our AI code for our little green team to make it move on command between these buttons.
So let's go back up into the assets folder and we're going to create a new C-sharp script.
And this C script will call follow waypoints.
Okay, we're going to get that and drag it on to the tank.
So we're going to grab that and drag and drop it onto the tank.
So on the tank, let's click that lock that was on the WP major.
So here's our script here and now let's open it up and start adding in some code.
Okay?
So leave it as a mono behavior because it does need to run like that because unity will be running it.
Let's go transform goal.
So this will be the transform of the goal so that we can look and move towards the goal.
Float speed equals 5.0.
If of course you can change these, if you want the tank to go faster, we want an accuracy amount equals
1.00.0 f float rot speed equals 2.0 F.
Now this should be familiar sort of code because you've used it for following the waypoints when you
had all of those tanks going around the palm trees previously.
So public game object manager.
So this is our waypoint manager that will do all of the graph processing for us.
Game object waypoints to make the tank familiar with all of the waypoints game object current node which
will be storing where it's currently heading towards or currently is on the map.
INT Current waypoint equals zero and that will be the index of the current waypoint through the path
that is being calculated for it.
Okay.
And then finally we'll have a.
Graph G.
So in start, let's set this up with WP s waypoint system equals WP Manager Target component, WP Manager
DOT Waypoints and then G is going to equal the WP manager get component WP Manager graph and then the
current node equals WP SW and then you need an index number.
Now this is going to depend where your tank is starting from.
Okay, now you could calculate it, but instead we're just going to set it at that position.
So let's just flick back into our map.
Now a tank is over near eight.
What I'm going to do just to make things a lot easier is to move the tank so that it is over near Waypoint
one.
So just grab the tank, the W key and move it back over to here.
Now you can put it anywhere.
Of course, you don't have to put it at number one.
Now, number one in the array in our WP manager is at element zero.
So that's the value that you want.
So back in our follow waypoints, we'll put a zero in there for the starting point of our tank.
Okay.
Now, once we've done that, we can create a whole bunch of directives for that tank.
So let's go down here and we'll go public void, go to Heli, which is go to the helipad where we already
are.
Like this.
And this will be g dot a star.
From our current node to WP s zero, which is where the helipad is, where we're actually starting from,
and then we'll set current.
Waypoint to equal zero.
Now, this is not counting the indexes of the waypoints in that array.
When you're given a path of nodes to walk through, current waypoint is picking or pointing to which
node you're currently up to as you're moving through the path.
So the path might only have three nodes in it, and therefore this will just count up from 012.
And then it says, Oh, you're at your destination in that particular path.
Okay, so we've got that list now put in public void.
Go to ruin.
And I'd like you to fill this out as a challenge now what I call the ruin on the map.
If you're not familiar with that, let me just go back in here.
I call this over here.
Okay, so this is kind of ruin.
It's the leftovers of a building, and there's sort of a dead tank there.
Okay, so that's where we want to go to in that particular method, right?
So pause the video now and give that a go.
And when we come back, I'll continue typing that up for you.
Okay.
Welcome back.
How did you go with putting that code together?
Well, first of all, you need to know which waypoint we're going for, which is number two up in this
back corner, or at least it's two in my map.
It might be a different one for you.
Now, the index of that waypoint in our waypoint manager will be number one because this is zero and
then this is going to be one here, right?
So it's going to be one that we're heading towards.
Let's go back into follow waypoints.
So we basically want to grab this code, copy it and put it down into here and set that to waypoint
one.
Okay.
How did you go with that?
Now we're setting it back to zero again because that means we're starting out on a new path.
Okay.
So we're going to leave this video here.
When we come back, we will put together the code to move from a starting waypoint to an ending waypoint
along that path that is calculated in the graph script.