Welcome back.
No, you're not watching the wrong video.
I've just gone back into here to remind you of the code that we wrote for these tanks to go round these
palm trees.
Now, we originally had some follow waypoint code for the tanks themselves, and they would have an
accuracy of how close they got to the tree, and then they would sort of update to go to the next tree
in the particular circuit.
And then we updated that and went to a progress tracker where the tracker actually went around in the
tanks, followed the tracker.
What I'd like you to do for this particular video is cast your mind back to when we had just the tanks
going around between the trees without the progress tracker, because we're not going to use the progress
tracker in this particular case.
And it will also give you good experience of writing code to get something to go between waypoints.
Because trust me, I have done it I think a million times.
So what we're going to do is remind ourselves of that so you guys can go and find where yours is and
open that up.
I'll just put that out of the road for now.
Now, we're going to go back into this project.
And first of all, we're going to go into the graph code because I want to make a slight change in here.
Path List is going to be your list of nodes that you should follow in the tank to get from one location
to the other.
Okay.
Now, we're going to need this back in our follow up code because it's what's going to be following
these nodes along.
So I'm just going to make this public for now because that's just going to make life a lot easier.
If you want to add methods for getting the path list and returning it, that's completely up to you.
But this is just easier.
So we're going to save that in here in the graph.
Now switch back into follow up.
What I'd like you to do as a challenge.
Yes.
Is make this a late update for a start.
And then within here write the code so that if we go go to heli is run, then late update is going to
go through the process of moving from one waypoint to another.
And in fact, just to get this to run to start with, up in the start, we're going to put in an invoke
and we'll invoke go to run in 2 seconds.
We're doing that so that the rest of the code and rest of unity has timed actually populate all of the
things in the inspector that it needs to do.
Because if you run all of this in the start and then you're trying to go go to ruin, it could be that
the waypoints have not been currently set up in the waypoint manager and that this will fail because
it can't find the starting and the ending positions.
Trust me, I know because I actually tried it and it didn't work.
So that's why I put the invoke in here for 2 seconds.
Now the other thing you might like to do too, so that you don't end up spinning around and ran around.
One waypoint is come up to the accuracy and change that to a five.
I found that actually works a little bit better for problem solving this.
Okay, so we're going to save this.
Now it's your turn to see if you can write that late update code.
So pause the video now go give it a go.
When we come back, I'll step through it with you.
Welcome back.
How did you go with.
That was a big task.
Now, there's numerous ways you could lay this code out to get it to work.
I'm just going to show you my way.
You might come up with something completely different, which still works quite well, and that's absolutely
fine.
Okay, so what are we going to do?
First of all, is check whether we have a path that we can follow and that we're not already at the
end of the path that we're following.
So if G dot path list dot length count equals zero or current waypoint equals.
So the index of the current waypoint equals g dot path list.
Dot count.
If that's the case, then we're just going to return because we've got nowhere to go.
We're either have no path or we're at the end of the path we were following.
Okay, so now we're going to test whether we've got close enough to the waypoint we're heading towards
so that we can switch to the next waypoint.
So if vector three dot distance of g dot path list at position current WP.
Dot transform dot position.
Now this is a path list of nodes.
Now this will be a dot get ID on it because that's what's got the game object on it.
We want to make sure that we turn the node into the game object in order to get the position.
Now the distance between that and where the tank currently is, which will be this dot transform dot
position and that is then going to be tested against our accuracy, which is the same thing that we
did where we had the tanks racing around the palm trees.
Okay, so if this is the case, we've got close enough to the waypoint we're currently going for so
we can go to the next waypoint.
Now, if we aren't at the end of our path, if we're still following waypoints around, we're going
to go if current.
Waypoint is less than g dot path list dot count.
Then our goal will equal g dot path list at position.
Current waypoint dot get id dot transform vector three.
Look at goal.
This is the direction we need to look to be looking towards so we can actually slurp our rotation around
for our tank.
New vector three go dot position dot x.
And then this dot transformed position dot y and then goal dot position dot z.
The reason that we're taking the tanks y position is so the tank doesn't actually tip forward or backwards,
so it stays level on the Y.
Even if you have put your waypoint positions slightly up or down from the tanks y position, this will
just make sure you keep the tank level.
Okay.
Now vector three direction equals look at goal minus this dot transform dot position and then we can
do our rotation.
So this dot transform dot rotation equals turn in dot slurp this dot transform dot rotation and could
turn in dot look.
Rotation in direction.
And then our speed, which will be time, delta time multiplied by our speed value.
And then we simply just push our tank forward.
So this dot transform.
Dot translate.
Zero zero spade multiplied by time dot delta time.
One little thing I forgot to put in this code before you continue onwards is to put in this line here
so that when you reach a waypoint, then this current node gets updated because it's this current node
that is being used to basically set the path starting position in all of your go tos.
And if you don't have this, then it just won't act correctly.
So you want to set current node to the current node that you've actually just found, which is with
this distance thing, then you can update current waypoint to be going to the next one and when it finds
the next waypoint, it will update that for the current node as well.
Okay, great.
Save that.
And now we're ready to test it out.
So let's go back into unity.
Okay.
So now what we need to do is get our tank or is it over in the hierarchy and we have this WP manager
exposed here.
We need the WP manager in order to get hold of the graph and the path list and all of those things from
there.
Right?
So let's press play and we'll be able to see our tank moving.
Let's just zoom in in the scene view here so we can see what's happening.
So we're going to start over at Waypoint one now after 2 seconds, we're going to start on our route
and we're going to go this way.
Well, I shouldn't predict where it's going to go.
It's going to take the shortest path that it found.
Of course, it could have gone around this way, I believe, as well, and come up to there.
So now we are at here.
So we can actually program at this point to go back to the helipad.
So it should turn around and follow the points back and we'll come back and put some little buttons
for commands in in the next lecture.
But in the meantime, you might like to program in some other destinations.
Okay, so we've got go to Heli, we've got Go to Ruin.
We could have something like go to what are these oil pumps or factory or whatever it is, go to factory,
go to over there.
It's up to you.
So just put those in.
And that would go into our follow waypoints code with our other go tos in here.
And when we come back in the next lecture, I'll show you how to put some really quick buttons up on
the screen to call of these and get our tank moving around the environment.