Welcome back to Part 2 of this lecture in creating waypoints and A* pathfinding.
So far we've created a map with a tank which is our NPC and a series of waypoints that we've created
edges between to construct a graph.
We've also added the graph code in, which contains an A* algorithm and we'll be able to ask the graph
to find us a path between one and another of our waypoints. In the FollowPath code that is attached
to our tank,
there's two public functions.
One is to go to the helipad or GoToHeli
and the other one is GoToRuin, which will take us up to the back of the map where there's a ruined
tank there.
Each of these runs the A* algorithm from our current node to a specified position based on
where the waypoint is, in the waypoint array.
Now our have job is to call these functions from outside of here and use buttons.
So with all that done and ready to go, let's go over to the Hierarchy and we'll just right-click, go down
to 'UI' and find 'Button'. That's going to create a Canvas, a Button (obviously) which will be childed
to the canvas
and this EventSystem.
Now this button will appear in the scene.
If you go to the 2D view and just zoom out a little bit, we'll find the button...there it is and it's showing up
on the canvas.
This white frame shows you the extremes of what you've got in the Game view, which is here. You can see the button
there and the scene itself is down here.
If you haven't used the UI system before, just note that the scene itself (the 2D scene) and the 3D scene are
not related to each other
in this view. So don't think that you need to move the button and stick it over the top of the landscape
because that's just not how it works.
This button is on this screen, within these dimensions.
We're going to move it up into the top corner,
so just select the button and click on this Rect Transform location box and we want to click the
top corner and we want to hold down the Alt-key at the same time and click it again, which will reposition
the button into that top corner.
Now I don't want that button EXACTLY up in the top corner, so I'm going to select it and just move it
down a little bit like that, so when you look at the game view it's not jammed right up there, it's right there.
All right.
So the other thing we want to do is change that text. Under the button, if you click down, there's a text
element
and this gives us the text showing on the button.
This will be 'Go to Helipad' and that's what it's going to do when we click on it. Before we can have the
button run the code that we want it to,
that code needs to exist in the Hierarchy somewhere and currently, it doesn't. The FollowPath code is ready
to be attached to our tank. Select your tank, drag-and-drop the FollowPath onto it.
Now if you remember, there's a waypoint manager reference that it needs to have, so that it can
grab hold of all the waypoints and the graph etc.
So just get your WPManager and make sure you connect that up to your tank.
With that done, we can now go back to the button and at the bottom of the buttons Inspector, there's an
'On Click' area.
Now it might look like this list is empty.
Click on the '+' sign and then we want to select the object that has that code.
That's that tank.
So put the tank down in there and we select the function.
So drop down this box and you'll see that it's got FollowPath attached to it.
Now in the list (that's just off your screen - we can bring this in a bit)
'FollowPath' and then it'll show you whatever it can run that's associated with that FollowPath code
and you'll find your GoToHeli and GoToRuin functions in there.
So we can just select that GoToHeli
and now when that button is clicked, it will run that function on the tank, which is so nice and convenient.
The last thing we need to do before pressing Play, is make sure that we'll be able to see what's going on in
our Game scene. You can see here that the game is currently not looking where we want it to.
So I'm going to go to the Scene and go out of 2D view, find the tank and then scroll out and
just manipulate this around.
I just want to be able to see what's going on when we run it, basically.
So there we go.
Now we'll select the camera and go 'GameObject > Align With View' so that the game is now looking
at the scene where we'll be able to see what's going on.
Right so now press Play.
Nothing will happen.
Notice that waypoints all disappear
and that's because the graph code itself will grab hold of all of those nodes and turn off all of the
things that are showing up, so that you're not going to see them in the final running of the game.
Press [Go to Helipad] and you can see that the tank goes to the first waypoint that it needs to because
it is closest to here
and then it will take the path directly from there to there
and when it gets to the heli, it will stop because it's got nothing else to do from there.
Ok.
So let's now program to go to the Ruins. The process is the same, grab hold of the button on the canvas and
just press Ctrl-D, which will create you a duplicate button. Go to the Scene and go to the 2D version
of the scene,find that button, now to select it just click on this icon here, so that you get the little
blue dots around it. That allows you to resize it and move it. Make sure you're moving the right one. Move that down
just underneath it and while we're
there, we're going to change the text to Go to Ruin.
Now it's going to have an 'On Click' on its button, so you go up to the button.
It's got the 'GoToHeli()' on it because we duplicated it from the previous button.
Now we can just find the other function, which is 'GoToRuin' and add that in there instead.
All right, so now let's press Play and we should be able to go to the helipad, which will take our tank
first of all to our current way point down to here,
at which point it will set that to the current waypoint...
and stop and then click [Go to Ruin].
Now it's not going to take this path back up to there because it's much quicker for it to go this way
because of the distance that the A* is calculating and you can see now that it's following the
path along, there was a waypoint there, there's a waypoint over here that it has to get to, to avoid these
rocks
and then it will go to the waypoint up there.
Ok, great.
So that's working.
Last thing to try is the point that we had over here where there's only a one-way connection.
So we'll duplicate the button for Ruin, move it down and then we'll change the text to 'Go To Tanks'
and for its button, it's going to run a function
we haven't written yet.
So we need to go back into our FollowPath code and we're going to copy one of the 'GoTo' methods that we had
before
and paste it underneath and this time we will 'GoToTanks' and the tanks are at waypoint number
9 (which is 8 in the array) and we make sure that we set the current waypoint back to 0 before
we get going.
So save that, switch back into Unity and then grab hold of that button and set its function for the
one we just wrote, which was 'GoToTanks'. With all that saved,
we're ready to play.
So let's begin by going straight to the tank.
First of all, it has to go to its current waypoint, which is this first one up here
and then it will follow this path that it has.
It can't zip across to here because it has to come back to this one that's down there, which is our intersection.
Now it's going to come around and end up down near the storage tanks. At this point,
let's say we want to go to the Ruin. The shortest distance would be that way but it can't go back along that
path.
So let's have a look what happens.
It's going to go around because it's found a path that will go around
and then it will come back and join up with that waypoint that's just here, to then take the one here
and end up over there.
So that's the A* algorithm and waypoints and you can see how powerful it is. It's really quick to
calculate distances and you can use these for navigating around all sorts of different terrain.
So you know, have fun with it!
Play with it, put your own maps in and see what you can come up with.