In this video, we're going to complete our exercise with these waypoints by actually giving commands
to the tank so we can see it going between the different locations.
So what I've done so far with our project that we've created is I've set my camera view to full HD.
We're about to put some buttons on the screen, so having this sort of set to a specific resolution
really helps.
So you'll want to do that.
And now we can create the methods that we want to be called by our buttons.
So if we go back into our follow waypoints we've got so far, go to Heli and go to ruin.
I've also added in a go to factory, so you might want to add in as many of these go tos as you like
to test these out.
Now the factory node in my example is at the waypoints for position in that array.
So how do we hook these up to a button?
Okay, we're going to go over into the hierarchy, right click in here.
We want to go down to the UI and you're looking for the legacy.
We want not going to go into these other things because the legacy is just way quicker and we're not
looking at creating fancy UI's at this point.
So it's just off the screen for me.
Let me just.
Bring this in so you can see it.
Right click.
Create UI and legacy, and then you want to grab the button just there.
So there's one button.
Now once you've got that button, we can go over into our scene view and if you click on the 2D and
then double click on the button and then zoom out, you'll be able to see where the screen space is.
If you're not familiar with the UI system, then you can see it in the 2D view in the scene.
And this is the actual size that we set our canvas to.
So that HD setting where the button is here and where your 3D scene is, you can see it's very, very
tiny down there.
They kind of have nothing to do with each other, so don't get distressed that this is absolutely enormous.
But the button, if we just bring this out, you can see that it's now sitting in the middle there.
The first thing to do in your canvas, if you click on it to make this button scale with the screen,
is to come over into the inspector and just go down to canvas scaler and select scale with screen size.
Okay.
So now we can actually bring this back in here a little bit and we're going to select the button and
then over in the inspector, you want to click on this thing here to bring up the alignment.
You want to hold down your option or your alt key and then just click on that there.
Now that's going to move it up into the top corner.
From there you can actually selected hit the W button if you don't have your axes and I'm having a bit
of trouble here and then you can actually just move it like that.
Okay.
So I just don't want it smack bang in that top corner.
Okay, let's get that saying something.
So over in the hierarchy, drop down the button, you'll find that there's a text area there.
That's where you can write the text on this button and we're going to put in here go to ruin like that.
Okay.
So it's going to change your text here.
We now need to link the button up with our method in our tank.
So select the button in the hierarchy and then come down in the inspector.
You're looking for the button element in there component and we're going down to the on click, so go
on.
Plus you want to find a function that you can run.
Now the function that you can run is in our follow waypoint code, which is attached to our tank.
So we grab our tank and we put that down there and then if we drop this down, you'll see that there's
follow waypoint in there.
Now I need to bring this in again.
So you should be able to see that and follow waypoint.
And then in here you'll find a list of a whole bunch of different things.
But any public methods that you've created in your script you will see here.
So you've got go to factory, go to Heli and go to ruin and go to ruin is the one that we want here.
Okay.
So that is now ready to go.
So let me just bring this screen out so we can see it press play.
And once that starts up, we can hit go to ruin and he will immediately go to the ruin.
Now the other thing that I haven't done is get rid of that delayed start for the go to ruin in the scene.
But anyway, you can see that's working there.
Let's go back into our code here and remove that so it doesn't automatically run.
Now, before we continue adding more buttons, we need to actually check that if our destination is
where we want to head to, then we don't need to find a path currently as it is.
If you select a starting and ending destination that are exactly the same, it's going to fail at finding
a path and it will then give you the previous path because the calculating path which is down here,
reconstruct path won't run again.
And that's what's doing the clearing.
So what you can see that I've done, if I come up into here, I've got some de bugs because I was trying
to sort out this issue at the top of the star.
You want to add this in here?
If your starting and ending points are exactly the same, then you just want to clear the path list
and return false, which is going to stop your tank from doing odd things.
All right.
So we're all ready now to add some more buttons for all of these.
Now, I've just got to add go to hell and go to factory, but you might have some more that you want
to add in.
So this is your challenge.
What I'd like you to do is go ahead and add those buttons yourself so that can get your tank to navigate
around when you click on them.
Pause the video.
Now go do that a go.
And when we come back, I'll step through hooking up my other methods.
Welcome back.
How did you go with that task?
Well, let's create two more buttons for mine, because I've only got two other methods.
I'm going to select this button and duplicate it.
So control D and then just move that one down and then duplicate it again and move that one down.
Now, this button here, if we come over and find it and the text will put this one to go to helipad
and the next one at the bottom that can be go to factory.
Okay.
So let's go back to the helipad button.
And if we look down through the list because we copied it, it's already linked up to the go to ruin
from the go to ruin button.
So all you got to do is actually just select that and go into your follow waypoint.
Again, that's off the screen.
Bring this in here and follow Waypoint.
Now, this is the helipad.
Go down to the go to Heli for that one.
And while we're here, we'll do the other one as well.
So that one will be go to factory like that.
Okay.
So we can now actually run this and have a look at the behavior of the tank.
So let's press play.
Okay.
So we're going to go to the factory.
And yes, if you noticed, I did fix up my spelling mistake I had in that button.
I've also sped up the time this takes to actually run with a time.
Time scale equals five.
Put in any of the starting methods that are going to run, which you'll just make testing a lot easier.
And now let's go to the ruin and ruins up in the back corner.
Now, if we try and go to the same place again, it won't let us.
But we can go back to the helipad and then we can go back to the ruin and we can go to whatever station
that you set up in there for your waypoints.
Now, the important thing with this is that the tank stay on the path denoted by the waypoints.
For example, if you put your tank over here, it's not near any particular waypoints.
And if you want it to get to the closest waypoint, then it might actually have to go through geometry.
And that defeats the purpose of why we have the waypoints in the first place, which is to essentially
allow it to navigate without seemingly colliding with anything.
So that means you really want to have the tank start on one of these waypoint nodes.
So if it's at position one, as I said in the first video for this section, then you want to make sure
that the code has it set up being at position one.
So if we just go back to its code in follow waypoint, let me open that up.
Up in the start where we set the waypoint that we're on.
So Waypoint zero in this case, that's because we're at Waypoint zero here.
But if we wanted to put our tank on the other side of the map, so let's just grab our tank and drag
it over.
Oh, not the top of the tank.
Let's just grab the tank and we'll drag it over to here now because we're over here, we're starting
at whatever that waypoint is just there.
So let me zoom in and have a look.
It's Waypoint five now in our waypoint system.
Then we need to find out which one that is.
So in our manager five is in position five.
So this was element four in that list.
So it's woops four so it means we go back into our code and just set this to starting at position four,
save that.
Then if we go back into unity, we can now press play and we're already at position five when I think
I've got that as my factory.
So if we hit go to factory, it's not going to go anywhere because it's already at the factory.
So we can go to the helipad from here like super fast, and then we can go to the ruin and we can go
wherever we like as long as it's on the path.
And that is the most important thing about having a waypoint system is you don't want your tank going
through any of the geometry.
All right.
So that is setting up waypoints and travelling between them using the estar algorithm.
Now what we're going to do a little later in the course is actually look at the nav mesh system that
comes with unity.
And that nav mesh system to get from one place to another actually implements a star behind the scenes.
So you don't need to code it.
And for doing this exact same exercise, we can use the nav mesh instead, which just gives you a little
bit more power in moving agents and working out the size and having multiple agents and not really having
to worry about them colliding or any of the other sort of physics that might be involved with you having
to do it manually like we're doing here.
So that's the end of this section and we'll come back and start looking at NAV measures and what they
are and then how waypoints fit into that in the next section.