In this lecture, we're going to start writing the code to drive the car with a artificial intelligence
controller.
So what we need to consider is the forward direction of the car and the vector to the waypoint.
From that, we can work out the turning angle that the car needs to turn to start facing in the waypoint
direction.
And to do this, we can just use some simple high school trigonometry.
So if you remember your rules about tan, cosine and sine for finding the angles of a right angle triangle,
you can actually construct a right angle triangle like this where you've got your X and Z values, which
is basically the distance between your vehicle and the next waypoint.
Now, using a ten two will allow us to also find the full angle based on the quadrant which the angle
appears in.
Now, I won't go into this mathematics for you, but if you'd like to have a look at the attached resource,
it goes through a ten two and how that slightly differs from ten and why we can get away with finding
that full angle when the angle is not actually inside of the right angle triangle that we constructed.
All right.
So let's get started with the script.
We're going to create a new C-sharp script and we're going to call it A.I. Controller.
And that A.I. controller is going to go on to our car.
So once that finishes compiling, just drag and drop it over here onto the car and then just check that
it's there.
Right, let's open that up.
So it needs to know about the drive script so they can actually call the go method for those.
So in here we're going to first add a drive array of the drive scripts like that and then in the start
we'll grab all of those.
So drive scripts, DS is going to equal this get component in children drive.
So that's just going to automatically pick all of those scripts up for you and make sure that you use
the plural version of it, because we won't get components into the four of them or however many wheels
that you've added to your particular vehicle.
Okay.
So besides all of those, we also want to get the circuit.
So we're going to put public circuit circuit.
And this will then point to the circuit that you've created for the car.
We also need a public float for steering sensitivity and we'll set that to equal 0.01 F initially.
Then we need a vector three for the target that we're currently going towards and we need an INT for
our current waypoint.
Now you've seen this before because we've actually looped around waypoints with our tanks that we've
done.
But in this particular case, it's a little bit different because we're steering towards the waypoints
rather than just slipping the entire vehicle across.
Okay.
So what we want to do in the start, to begin with is set our target to the first waypoint so it'll
equal circuit dot waypoints, current waypoint dot transform dot position like that.
Okay, so just save that and we'll just switch back into unity and just set these things up.
So with our car, we've got our circuit here, let's just drag and drop that into there so it can pick
up those waypoints.
And for a moment, let's go back into our drive script, okay?
Because in here we want this code.
Let's just grab this because this is not going to be used any longer inside of the drive.
So we basically want the middle of this, but we're going to just then comment out this entire update
because we don't want it running independently.
Okay, so save that switch back to our controller and paste your code into here.
Now this go needs to run on all of the drive scripts up here, so we're going to have a little for loop.
So four int i equals zero i's less than d's dot length.
You could also use a4h.
I guess I'm just more familiar and go straight for a normal for loop when I'm coding and let's put this
like this and then we need to get.
Index of I dot go and run that go now you can see it's not liking it just here if we switch back to
the drive script and you have a look at the go, it's not public.
So we need to make that public so we can access it back in.
I Controller So save that.
Let's go back into here and you'll notice that it's no longer read so you can save that and that's going
to run our car now the A and the SE we're no longer going to use.
We actually want to program those values in.
But with this in here now we can actually check if it's still working correctly, like the wheels and
everything are going to.
So we're going to now press play and just check that we've got all of that set up correctly.
So the wheels are turning and we can still go forwards and we can turn.
So those commands are getting through just nicely.
Okay.
So let's now continue looking at our air controller.
So let's get rid of these values or in fact, let's just leave them in there because instead of using
the get axis and the vertical and horizontal for those, we're going to program them based on the angles
that we need to turn between our waypoints.
Okay, let's get the rest of the code in.
The first thing we're going to need at the top here is a rigid body for the rigid body that's attached
to the car because we need to know how fast it's going, which is its speed.
So in the start we'll go our be rigid body equals this dot get component rigid body.
Okay, now in the update, let's come down and actually make the modifications that we need in here.
Let's leave our acceleration and our steering in here as they are for now.
But we'll go to work out the angle between the direction the car is facing and where the next waypoint
is.
And we have to do that in the local space, local to the actual car.
So it means that basically the local value for the forward of the car is always going to be considered
as it's zeroed angle.
And then you've got to see how much it's got to turn from that position so we can get the local target
from this dot transform dot inverse transform point, and that will get the target relative to the location
of the car.
So it doesn't matter where it is in the world or which way it's turned, it's going to be just local
to it.
So it considers the car basically at 000 and then we go float distance to target equals vector three
dot distance between the target.
And they transform.
Dot position.
And then we can calculate the angle.
So float target angle equals math f dot.
A ten to local target, ex local target.
Dot Z multiplied by maf f dot rad to degrees because unity works when it does any angle calculations
or angle functions works in radians and we need it in a well, we don't really need it in degrees.
It just makes more sense to us when it's in degrees.
Now, the acceleration, we will just put that as 0.5 F, we're just going to set it to a static acceleration
and the steering.
We're going to calculate that from our angle.
So in here we'll go math f clamp.
We're going to clamp the target angle multiplied by the steering sensitivity.
We're going to clamp it between minus one and one, just like we did previously, because we basically
want this to be a percentage of -1 to 1.
Now, when we're working out the angle, it's going to be determined on which way we're actually facing
or which way the car's facing.
Because if it's facing, for example, up like this and you want to turn to this one, then you actually
have to turn anti-clockwise.
If the car is facing down and it has to go over here, then you actually in the world you have to face
or rotate clockwise.
So it does matter which way the car is facing and we can get that from the actual speed of the car,
which is why we added our rigid body so we can go math f sine.
We only even want to know the sign of the speed.
So rigid body velocity magnitude which will be the speed, whether it's travelling forward or backwards.
And once we've done that, we can then loop around and actually put everything through to our wheel
drives.
After that, we need to update our waypoints.
So just a simple if statement here, if distance to target is less than two.
So if we get within two of this particular waypoint, current waypoint will increase to the next one
in the list if current waypoint is greater than or equal to the circuit waypoints length.
So we've run out of waypoints.
Then we need to just go back to the first one again because it's a circuit, so current waypoint will
equal zero and then we can go target.
Update that target equals circuit dot waypoints current waypoint dot transform position like that.
Okay, so we're ready to try this out.
And I've just noticed I have an extra bracket floating around the bottom of my class.
Let me just get rid of that.
Okay.
Otherwise, the code is good to go.
So let's save that switch back into unity.
And now once you're in here, these waypoints are cubes, so you want to select them all and, and tick
the box.
Collider Okay, otherwise your car is going to run into them, which you don't want later on.
If you don't even want to see the boxes, you can turn off the box.
Collider I always use a cube so that I can just see a location in the world which helps for debugging
purposes, right?
Let's press play and have a look what happens.
So all the car is turning.
Now, where is it going to go to?
This is Waypoint one.
If I select it, you can actually see that it's just there now.
It's trying to get to 8.1 and it's going round waypoint one and will it go to a point two?
Now it's circling around.
Now this is a common issue that you have with waypoints.
And can you remember why we've already covered this in other waypoint tutorials?
So this is the challenge.
So if you can just fix that before we come back and get the car to go around each of these waypoints
pause video.
Now, when I come back, I'll continue going through that with you.
Okay.
Welcome back.
How did you go?
Well, the reason that a NPC is going to circle a waypoint is because it can never get close enough
to the distance value here to be considered close to it.
So I'm actually going to change this to for like that instead.
So we can definitely get within four of this waypoint, save that, let's go back into unity.
Of course, you can also increase the steering sensitivity and the maximum turn on all those sorts of
things, which would also influence on that.
But this is a car, so you don't really want it to turn too sharply.
Okay.
So now that it's considered that it has reached Waypoint one, which is this one here, the second waypoint
is this one here.
So off it goes down to there.
Then once it has, it's going to turn towards Waypoint three and go through that one.
And then eventually off it goes up to the fourth one.
And so now it's actually nicely going around our circuit.
Okay.
So that's the basic waypoint system and steering in place.
When we come back in the next video, we're actually going to work on enhancing this code for our AI
controller.