We begin this lecture where we ended in the previous one, with our orange car that is following our
tracker around the track and also braking and accelerating.
Now we want to add in an opponent, so let's go up into the Hierarchy, grab hold of the orange car and
then Ctrl-D to create another one.
Let's just change the name of that and call it BlueCar
and then in the Scene, if you hit the W-key, you should be able to drag that extra car out...like that.
Now this is our blue car.
It's not very blue.
So let's create another material for it. Now if you have a look down in the Assets, there's
'CartoonCarFree > models' where the car model is
and then there's a material.
This material here, the CartoonVehicleAtlas, just Ctrl-D on that, which will give you a second
one.
Rename that to BlueCar, so we know which one it is, then with it selected, over in the Inspector you
want to set the main color to blue...
something like that.
Right.
So with that new blue, you can drag that over and just drop it onto the car. It's probably not dark enough.
It's just made the windows a bit 'purplish'.
Now you can change it with the car in the scene. It's because the orange is already in the texture
that it's going to be a bit odd. If you want to make it a true blue,
then you can take this texture
here, open it up in Photoshop, make the orange parts blue and then put it back onto the material.
But for now let's go with this.
Right.
So you've got the car separated, you've got exactly the same thing on the orange car and the blue car.
All you've done is make a copy.
Let's press Play and have a look at the behavior.
So the orange car (as it always has) is taking off...and off it goes.
The blue car has stalled completely.
If we switch over to the Scene, we can see that it's just sitting there looking at the cube and the cube's
not going anywhere.
Now, the problem with this is...well there's two problems.
First of all, the car isn't accelerating. It doesn't believe that it needs to accelerate and therefore
it's starting value of 0 for its speed is still set to 0.
Remember back when we first started working with these waypoints in the little tank and the tank
would be still...and stuck to the waypoint and wouldn't be going anywhere unless you dragged the tank after
its circuit tracker.
If I grab hold of the blue car, let's just see if I can get this to get going...there we go.
So now that's working because it added some speed in your calculations to the movement of the actual
car.
Now, what's happening? This is the problem that's happened...let's stop this. Press Play and then switch back
to the Scene view. Initially, our code is saying if we have a big angle between our car and the forward facing
vector of our cube, then you're going to put the brakes on.
If we have a look in detail of what we've got here,
there's our forward-facing vector for our car. If we click the cube, there's the forward-facing vector
of the cube.
So right from the very beginning, we've hit that condition that says put on the brakes because the angle
is too great between the car and the tracker.
To fix this, we go back into the code and where we're testing whether we should be braking or not,
we'll also test if we're moving at a certain speed because if we're not moving fast enough, there's no
need to brake. So we'll put an '&& speed > 10'.
Save that and go back and run
and let's see if we can get our little blue car to move.
Yes.
So there we go.
So both of them are now moving. If we go back into the scene,
we should be able to see them come around...here
they are. They're on top of each other.
They have collider but they don't have a rigid body, so therefore they are not going to cause collisions
with each other.
So that's easy enough to fix.
Select your orange car. In the Inspector,
you want to [Add Component] and add a Rigidbody and then you want to do the same thing with the blue
car. [Add Component], Rigidbody. To see exactly what's going on, you might want to go in and select the
cubes that are our trackers and turn off their mesh renderers, so they don't get in the way of the two
cars.
Right.
Let's press Play and have a look at the behavior now. So you can see that the rigidbody is going to
keep those two cars away from each other through the collision system.
We can now customize the behavior of each car by exposing all of those variables in the script and then setting
them differently in the Inspector.
The first thing you want to do though is go to the blue car and down where it's got access to the Readout
script,
just remove its reference to the Readout, so that the orange car is the only car that's updating
the speed readout,
otherwise you're going to be getting updated speeds from both cars.
Then in the code, you'll need to make sure that before you try and set the readout text, right at the
bottom, just put in an 'if(readout)' - if it exists, then put the text in it.
Of course, if you don't want to have the 'speed converter' or the 'readout', you can just get rid of the whole
system entirely.
Now at the top of the program, we can make all of these public, which will give you access to them in
the Inspector.
So I'll just copy-and-paste that in.
The only thing you don't want access to is the speed because that is what is being calculated. So save
that.
If you switch back into Unity, you'll be able to select either car and change all of these different
values. So you can change the acceleration and deceleration amounts, the minimum/maximum speed and also
the brake angle as well and see the different behavior.
So let's leave the orange car as it was before. With the blue car,
let's see if we can increase its acceleration to 8 and it's deceleration to 4
and let's see if we can get it to go 120 and we're going to make it quite cautious, so if this brake
angle will be 10 (I doubt it's going to get up to those sorts of speeds on this very short track)
so let's just save that and run it. Ok, so the blue car goes off, it's got a fantastic acceleration.
It's actually doing much better than I thought, I would have thought it would have slowed down a fair bit
around the corners and the orange car would have been able to catch up to it, but it looks like the blue
car might end up lapping it...
Yes, there you go.
So yeah.
Now at this point you can add a lot of different cars (if you want to) onto your track.
You might need to make a bit bigger track.
This is only a little one and you've got all these different variables that you can now change to get
different behavior out of the cars.