In this lecture, we're going to fix our waypoint following by adding in a tracker.
Right?
So in order to do that, you need to create an object that's going to follow the track around and then
have your tank follow that object.
So you want it like a super accurate progress tracker is what we're going to call it that stays on the
track and then the tanks actually follow it instead.
So go into your script and what we're going to do is create a new game object.
So at the top we're going to declare it so game object and we'll call it our tracker and then we'll
create this tracker down in the start.
So the tracker is going to equal game object dot create primitive.
Now I'm creating a primitive object so that you can actually see where the tracker is in unity.
Of course you don't need to see this position.
It's just a point that's going to be moving around the map, but so that you can visualize it.
We will make it, I guess, a kind of solid object.
So we're going to create the primitive of a cylinder.
So just select that from the dropdown list if you're in Visual Studio and then we want to destroy immediately
the collider that's on it.
So tracker dot get component.
Collider.
And make sure that that's a capital G.
For game object, create primitive, right?
The next thing we're going to do is set its position and rotation.
So tracker dot transform dot position equals.
Our tank.
So this dot transform dot.
Position and the rotation tracker transform.
Dot rotation equals this dot transform dot rotation.
Now the tracker, we want to move around the track using the original method that we had for moving
the tank.
So let's create a Void Progress Tracker, and this method will actually replicate what we had previously.
So first of all, it's going to test the distance of the tracker to the waypoint, and we've already
got that code sitting down here in the update.
So I'm just going to copy that as well as the length of waypoint.
So let's copy that out of there and put it down there.
And then what we're going to do is actually perform a look at again for it so that it is perfectly going
towards the tree.
So that would be tracker.
Don't transform.
Look at.
And we're going to look at the waypoints of the current waypoint.
To transform.
And then we're also going to move it forward.
So tracker dot transform dot translate zero zero and 0.1.
If this speed here, you can modify that based on how fast you want your tracker to go around the track.
But what you've got to make sure that you do is your tank and your tracker can't get too far away from
each other.
Otherwise, the tank, which is going to be following the tracker, not the waypoints, is going to
start skipping off the track and taking shortcuts, which is something that you definitely do not want.
All right.
So with that code, we're going to go down into our update and let's just get rid of this because we're
not going to be using that again and we'll put in here progress.
Trekker.
And we'll also just.
Comment out these lines of code just for now so that our tank doesn't go anywhere.
It's our progress tracker that's going to be moving.
And because it is the progress tracker that's moving, when we test for the distance in order to go
to the next waypoint, it's actually a tracker that we have to test the distance for.
So back up in progress tracker, make sure that you change that to tracker transform position is less
than three otherwise it's not going to continue around the track.
Right?
So save it.
Let's go back into unity now.
Once you're in there, you can see that I've already done this.
I've just left one green tank active.
So go through and select all of these other tanks that you've got in the scene and just tick their box
that's right at the top of the inspector, which will just turn them off for now because they've all
got exactly the same code on them.
And let's press play.
We'll see that the cylinder gets created and off goes our cylinder on its merry way around the track.
Now, because it's turning and perfectly looking at the palm tree or the next waypoint, it's going
to always hit it and it's still got a distance of three because it is perfectly turning towards the
position of the palm tree.
You could probably turn that down a little bit more, so you've got even better accuracy.
Okay, so you've got our tracker now we want to get our tank to follow that tracker instead.
Okay.
So back into our code and we're now not even looking at anything so we can get rid of that line that
we commented out much earlier.
Our concern in Look at Waypoint is actually now not looking at a waypoint.
It's going to look at the tracker.
So you want to change this to tracker here.
Then we can do our slurp of our tanks rotation, which is fine, and then we can push our tank forward
by the speed.
Okay, so that is fine as well.
So let's just save that and go back to Unity and have another little look.
We'll create our cylinder that's going to move along.
And you can see that the tank is now circling our cylinder.
Why is it doing that?
Well.
It's moving much faster than the cylinder is.
So therefore, we have to make our cylinder move faster than the tank back into our code.
So the progress checker we've now learnt needs to go at least as fast as the tank itself.
So let's put speed multiplied by time, delta time in here and let's just say speed, let's just increase
it a little bit.
So it's in front speed plus say two like that.
And because it's not actually turning, it should stay ahead quite nicely of the tank.
Save it.
Let's go back in and play again and have a look what we're getting.
So the off it goes.
Now you can see that the tank is following it nicely.
And that's not too bad.
It's taking a lot of shortcuts because of the actual distance between them.
So you could change that distance.
So that is a little bit shorter.
Now, what happens if your cylinder ends up going too fast for your tank?
So if the tank gets knocked out of the way by another vehicle and it ends up way away from it, it could
actually, instead of getting back on the track, take a shortcut straight across the actual track.
Let's go back in here and let's increase our speed.
Let's make it say 20 faster than the tank and go back and run that and have a look at the result of
your tank going around these trees.
Okay.
So now you can see that the tank is taking massive shortcuts around the trees because this tracker is
just getting so much further ahead.
So we actually want to reduce the tracker from getting too far ahead of the tank.
Right.
So back into the code, what we're going to do at the top is create another public float and we will
call it look ahead.
And we're going to say how far ahead of the tank will we actually let the tracker get?
So let's say that it's ten, ten, which is my go to value.
And then down in the progress tracker, we're not going to let it move any further unless it is within
ten of the tank itself.
So we'll do another distance test here if vector three dot distance between the tracker.
Don't transform position and this dot transform dot position greater than look ahead and an easy return
out of that.
So let's save it.
Go back and we'll have a look at what we get when we press play.
So the trekker.
You can see now and you saw it at the beginning.
Let's just get closer into here and have a look.
Is it will stop and wait for the tank to catch up to it.
And you saw that there.
So it's never no matter how fast you make your tracker, going to get beyond the tank, but you can
adjust it to be even further away if you want to.
So now that you've got that tracker on the code, it's now going to be on all of your other tanks.
So you can actually.
Put these tanks back into action.
And press play, and now you won't get any tanks that will circle their waypoints because they're following
these sort of perfect trackers.
The last thing, of course, that you need to do now is just go back into your code and make sure that
that cylinder doesn't show up in the scene.
So in here, what we're going to do is put tracker.
Don't get component.
Mesh renderer enabled equals false just to turn off.
That cylinder so that you can't see it.
Save that and press play.
And now we have a very basic set of waypoints, a circuit of waypoints and some different vehicles that
go around there, including the physics.
There's a little bit of sliding going on and some of the vehicles are being knocked out of place.
But that's up to you to experiment with now so you can change the rigid body settings, make it a heavier
vehicle so that when they crash they will actually go flying.
You might even want to remove the other physics restrictions that we've got on freezing the rotation
just to see how that will work.
But what you've now got is a good way of creating eyes that go around a circuit in a more believable
fashion than ones that sit there and circle around the waypoints.