In this lecture, we're going to continue working with waypoint systems
but I wanted to show you the more advanced car A.I. that comes with the Unity Standard Assets.
So I've started up a project here and added in my red spheres, which I have added to a WPCircuit, which
is an empty game object. All the spheres are children. To get the waypoint and the waypoint circuit
code and also the car, you need to bring in two imported packages. Up to Assets,
click on 'Import Package', the first one you want is 'Utility'. That has the waypoint circuit and the
waypoint progress tracker in it - just import the whole thing. The other one that you want to bring
in is the Standard Assets > Vehicles.
So add those two things to your project.
Next, we'll set up our actual circuit, so the script we want to look for is WaypointCircuit.
There it is
and I'll drag-and-drop that onto the WPCircuit object
and then remember that we [Assign using all child objects] like that and it's given me a very nice circuit which
is all joined and in order. Remember if yours is out of order, then you can reorder them here to get it to
the way you want it or you can physically move them around in the Scene as well.
Now the A.I. car that comes with Standard Assets (I've already found it here because I've searched for waypoint)
is called CarWaypointBased
and you can find that in Vehicles in Standard Assets. If you go down Vehicles > Car > Prefab
and it's there as well, so grab that car and bring it into your scene.
Now this car here is set up with a lot of complicated things, as far as the sound and the colliders,
so we probably don't want to shrink it or enlarge it to suit our circuit, instead we should change
our circuit and make it a bit bigger.
So I'll just quickly do that.
The car itself is grey and you might think that the textures are missing from it, but that is the actual
color of the car.
If you want to change that, you'll have to find the material and then put something else on top of it.
But for now we'll just stick with the default. Later on I'll show you how to use another car with this
same system, not just the Unity car.
Right, so select the car in the Hierarchy.
Have a look in the Inspector and scroll down and you'll find the Waypoint Progress Tracker
already attached to it,
so you don't need to add a new one.
It's wanting the circuit as we have used before and the circuit is the one with all the spheres in it, so we'll
drag-and-drop that down into there.
Now the car has its own 'waypoint target' which comes with it.
If you click on that, you can find it in
Helpers > WaypointTargetObject.
This car's quite complex, it's got colliders and lights and all sorts of things.
The actual model for the car is under SkyCar and it's the body that has the current shader applied
to it.
I guess if you don't want it to be grey, you could make it red or, let's go with bright purple just in there.
At this point we can press Play and have a look at the behavior of the car in the default settings.
After a while, it gets a little bit out of control and starts to miss the waypoints
and that would be because of its braking habits, its acceleration and all those things that it has programmed
into it.
You can see here, it's now quite erratic.
If we stop and go back and select the car and in the Inspector,
scroll down to the Car AI Control script,
you'll see all the different settings that can be used to customize your A.I. and if I just drag this out, I'll be able
to see them a little bit better.
So you've got things like Cautious Speed Factor
(so how fast do you want the car to go when it's being cautious) and cautious is what we talked about before
with braking, you know, when you're coming into a corner,
how much you want your car to slow down.
There's other stuff like Steer Sensitivity...Acceleration Sensitivity...all these things are defined
in the script.
If you open up that script and have a look, down here it will tell you in the comments what each of them
will do.
But we're not going to go through all of that.
You can read through it yourself.
The best way to determine what these do is to Play and watch the behavior as you change them. So let's
just press Play...
and we'll watch the car as it's going around.
So this is the default values that I had before
and I will change the Cautious Speed, turn that down.
You can see that the car has now slowed, it's much more cautious going into the corners and because there's
a lot of waypoints for it to hit,
it's constantly braking and slowing down. You can also change the maximum distance that you want to be cautious
at.
So we could probably put that down to 10, which will allow it to speed up more in between it and
only brake quite hard when it gets closer to a corner.
So you can see the different behavior that you got there.
Now you can spend a lot of time on your own playing around with all of these values to see what they
do
and you can add multiple cars like we did before, just by doing a Ctrl-D on this car and then
getting another copy of it and pressing play and you'll have two cars. In the code, if you're interested
to have a look, it has avoidance of other cars as well,
even though the physics system will stop them from bumping into each other.
So that's our purple car, let me delete that.
What we're going to do is use our own car model and your own car needs to be set up the right way.
The car itself (if we have a closer look at it) the wheels are separate objects to the cars
body and the script allows them to turn and they have different colliders on them as well.
So if you bring your own model in, it needs to have separate wheels and they also need to be facing the
same way.
So have a look at the axis system on this particular wheel.
The forward vector for the wheels needs to be pointing in that direction,
otherwise, you will end up with strange wheels that rotate around the wrong way, which might be the look
you are after.
But if you want a normal car, it has to be like that.
Now we can get a quick example of a car that fits our needs for this in the Asset Store
and this is the little free orange car that I use mostly for my examples.
If you read through the description, you'll see that it says it has separated wheels so you can animate
them with the vehicle simulation system, which is what we're using.
So let's just grab that car from there. You can also bring it in from the other projects that we've created
as well.
Now when that car finally comes in, in the Scene, we want to drag-and-drop that car in and put it over
the top of the existing vehicle.
So let me find it...CartoonCarFree > Prefabs
There it is.
Let's bring it in and put it with out other car.
Now it needs to be facing in the same direction,
so let me rotate it around.
We want to position it as a child of the CarWaypointBased object (that's the currently the purple
car) because it's got all of the scripts and colliders and we're just going to basically reuse them all.
So grab hold of your FreeCar and drag-and-drop it and make it a child of the CarWaypointBased object.
So you've now got the SkyCar and the FreeCar. With the free car
there, you want to position it at 0,0,0 relative to the purple car...like that
and we probably want to resize it at this point because it's a bit small, so let's just hit the R-key,
select our car and just make it a little bit bigger.
I'm looking for the wheels to be about the same size.
All right.
So from there I'm just also going to move it up a little bit.
Like that...ok,
not too bad.
Now the next thing that you need to do is to set the wheels up. The wheels are the driving force of the car.
The first thing we're going to do is align the wheel colliders and the wheel hubs with the wheels of the
orange car.
So we go up to the parent object the CarWaypointBased and have a look down through the Inspector and
you're looking for the wheel colliders, here and the wheel meshes.
So these are the colliders of each wheel on the purple car.
The wheel meshes are the actual wheels themselves
and this is the reason why they need to be separate objects.
Now these the SkyCars wheels. We're going to replace those with our FreeCar wheels. So, drop down the free
car here
and what you want to do is grab each wheel and drag it in to the correct position here.
So the first one it wants is the Front
Right wheel, so it's a matter of finding which one is the Front Right wheel.
In fact, I might just rename these...then when you've got them, go back to your parent object and you want
to drag-and-drop all of these into the right spot.
So the first one is the Front
Right
and then you've got the Front Left and then you've got the Rear Right and the
Rear Left.
Right.
At this point, if we Play, the car isn't going anywhere but not for want of trying. If we go over
to the Scene view, we can have a look at what's going on.
So let's just turn this around and zoom in.
So we've got 2 cars, the wheels for the orange car are now sitting over the top of the purple car wheels, which
is where we want them to be. So this vehicle system will move those wheel models of the orange car because
we have assigned them.
Now the issue of why this isn't going anywhere is because the orange car has a box collider on it and that
box collider (if we can get in close enough) is on the ground.
So it's dropped down onto the ground and it's basically stopping the car from going anywhere.
It's unable to push it forward.
So the easy fix for this is to select your FreeCar and untick the Box Collider
or you could remove it entirely.
Let's Play and have a look at this.
Now the orange car is going to drive and it's being driven by its own wheels on the purple car.
So let's pause that for a second and have a closer look at what's going on here as well. You can't see the orange
cars wheels very well because of the purple car, so select the SkyCar
and just untick it, turn it off so we can't see it.
As I said before, the wheels of the orange car are now sitting in the locations of the purple car and
we need to move those so that they sit naturally within the body of the cars model.
That's where these wheel hubs (that are part of the cars system) come into play
and there's four of them, that you can see there. If you select just one of them (let's hit the W-key so we
can find out where it is).
This is the Front Right and it's positioned near the front right of the tire...
but it's positioned near the front right of the tire of the PURPLE car.
We want to move it so that it's positioned on the front right of the ORANGE car.
The SkyCar itself is pretty superfluous at this point because we're not going to use it, so I'm just going
to turn that off.
You could delete it.
Let's go back to our WheelHubs
(this is our Front Right) and have a look at where it is. So it's positioned there.
We need to grab hold of it and move it, so that it sits nicely over our existing orange car tire.
You want to do that with each of these hubs, so I'll quickly do that...
All right so with that done, we can now press Play and just pause it again to quickly find our car...and
you can see that the wheels haven't moved away from the body of the orange car.
Finally, just for the physics system, if we grab hold of our car again, we're going to go into the Colliders
section, which is under the CarWaypointBased...the first one at the very top.
Inside you've got a ColliderBody, a ColliderBottom and a ColliderFront and they're all set up
based on the geometry of the purple car. If you want to fit them nicely over your orange car which will give
you more realistic collisions with this one, then you want to just modify them.
So this is the body of the car which is currently...there.
So let's just make that a bit bigger...
and move it up.
Then there's the front collider, which is on an angle that you can see there.
It's not too bad.
I might just move it up a little bit...and you can adjust that as you like.
Then there's the bottom skirt, which you can see there, which is quite low for the orange car.
Again you can move it up, so that it sits at the bottom...
like that and it probably needs to be just a tad shorter as well.
There.
All right.
So that is all of the setting up that you need to do for your own custom car in the vehicle simulation
system that comes with unity.