In the previous lecture, we added in some code to calculate the acceleration and the braking factors
based on the corner that's about to be turned.
Now, it's not good enough.
We found, if we're trying to go towards a particular waypoint and not looking ahead to the next waypoint
because it's really whatever's coming next, that's going to be the turning angle that we need to take.
So in that case, we can actually implement a progress tracker, which is kind of one waypoint ahead
of the car, and the car can follow the progress tracker instead.
So let's come up to the top of our AI controller code and we're going to add in a game object called
Tracker.
And we're going to have an inte current tracker waypoint equals zero and we want to float, look ahead
and we'll have that set initially to 40.
That's a that's how far ahead you want the tracker to be to the vehicle itself.
Okay.
So in start, we're going to create this tracker.
So the tracker is going to equal game object dot create primitive and I'm going to make a capsule primitive
dot capsule.
And this is just so we can see it and we're going to destroy immediate the collider on that because
we don't want to have the car tripping over it or getting stuck anywhere.
So we're going to get component of the collider and remove that.
Now I'm going to put this line in, but I'm going to comment it out because you can remove it a little
bit later.
And basically what it is, is the mesh renderer for the capsule so you can get rid of it when you don't
need to see it anymore.
So tracker get component and that will be a mesh renderer and we're going to put dot enabled.
Equals false.
Then we'll have tracker dot transform.
Dot position equals this dot transform dot position.
So we're putting it where the car is and then we going to have tracker dot transform dot rotation equals
this dot transform dot rotation.
So it's facing in the same direction as the vehicle.
Okay, great.
So that's setting up the tracker.
Now let's create a progress tracker method, I guess, void progress tracker.
And this is going to basically get our progress.
I got to go around all of the waypoints.
So we've already been doing this with the car.
We want to debug dot draw line and this line is so that we can see which capsule belongs to which car
and also the angle between them.
So this dot transform dot position and tracker dot transform.
Spell that right tracker dot transform dot position.
Alright then if vector three dot distance this dot transform dot position tracker dot transform dot
position is greater then look ahead.
Then we're going to stop our tracker from moving.
So the look ahead is having our tracker on like a piece of elastic and it can only get that far away
before it stops and waits for the car to catch up.
Okay, so a tracker transform look at and we're going to look at the circuit dot waypoints in brackets.
The current tracker waypoint dot transformed position.
The Troika has the luxury of using a lockout, which is going to snap it around and look straight at
where it needs to go, which means it's going to end up getting quite close to where it is.
So we don't to worry about slurps and all those sorts of things making the performance inaccurate.
So Tracker Transform, Translate and zero zero.
This is how fast you want your tracker to go.
Now if you find that the tracker is like really quite close to the car and stuck with the car and never
gets a far enough away, then of course you could increase this value.
I've found with the current settings it seems to be just fine.
Okay, so we've got that and that's pretty much all we need to do for moving the tracker.
And then if vector three dot distance of the tracker dot transform position and the circuit dot waypoints
current tracker waypoint dot transform dot position is less than one means.
We've hit that waypoint and so we're going to update it.
Let me just see if I can put this line down here so that you can see a little bit clearer what we've
done.
Okay.
So after this inside of here, we'll have current tracker waypoint plus plus.
Then if current tracker waypoint is greater than or equal to the number of them.
Circuit dot waypoints.
Dot length.
Then current.
Check point equals zero.
Like that.
Let's get rid of that space in there.
And then we've got that bracket there to finish off our progress tracker.
Okay, great.
Now this has to actually run and stay ahead of our car.
So we're going to update its position inside of the update.
So the first thing in here we will do is put progress tracker and just run that.
And then after that, we're going to set the target for the car.
So the target, rather than being a waypoint, is going to be the progress tracker.
So tracker dot transform, dot position.
All right.
So the target now gets used to work out the distances and the angles, which is all good and everything
else is good until we get down to the bottom where we're updating our waypoints and our targets for
our AI controller.
And let's just comment this out because we don't need it any more.
We're going to follow that waypoint tracker instead.
Okay, so let's save this and we'll go back into unity.
And what I'm going to do is just go through some of the settings I've been playing around with on my
wheels, which might also influence how it behaves and how yours behave.
So again, select all your wheel colliders.
My maximum torque is at 300, my max steer angles at 90, my max break talk is at 5000, and in my other
settings I have not changed the friction or I did actually play around a bit with the stiffness of it
and I found that I'm just going to stay with one.
The other values I haven't changed and they should be like you already had.
Okay, so now what we're going to see when we press play is a capsule pop up that the car is going to
follow.
So let's see how it performs.
All right.
So off it goes.
Now, notice it gets so far and then it just, like, waits until the car catches up.
And then once we get up here and it starts to go over to this waypoint, you'll see that the car actually
will break a lot earlier into that corner.
And off it goes.
And it's not too bad what I've achieved with this so far.
And that was mostly fiddling around with these values, which is what you will have to do.
Okay, so we're coming up to this corner here and the waypoint tracker is going to be over there before
it actually.
And now it's actually flipped and then it's going to land upside down because that's it's flip and off
it goes again.
So not perfect, but getting better.
And you can, of course, play with the distance between your waypoint tracker and the vehicle.
Okay.
Now, one thing that is a bit of an issue is not in this corner, but you would have seen up the other
end is that the car flips over because it doesn't seem to have enough sort of give in the suspension
at that time on one side.
And therefore, it just flips.
And we're coming up to flip the car again and notice that when it does do that, it just it just dips
straight over, okay.
A normal car is going to sort of slide on one side.
The wheels might lift up on here and these will actually take on a bit more of the pressure.
So we'll come back in the next lecture and look at how to implement anti rolling code.