Okay.
Welcome back.
We've put the cars in.
We've got the collision and the avoidance stuff on there.
When you pressed play, did you end up with a pile of cars like I'm about to and some weird performance
from things?
So let's have a look at.
Okay, I'm going to pause it.
Some cars have escaped collision.
Some cars are still trying to push their way through and some of them will eventually start going around.
This car is a concern.
Look where the little tracker ended up.
So what on earth happened there?
Well, I can tell you what happened was an oval locus of code that I came up with.
So let's fix that one first with that, because the cars kind of got stuck, right?
The progress tracker ended up being near the car all the time or ahead of the car.
And what happens if these two lines run more than they should?
Pass a certain point.
Okay.
The track of speed could actually go into negative.
Now what that's going to do is place the tracker behind the car.
Or in the other case here, if we're going in the other direction, it could also put it kind of behind
the car if it's sort of going too fast.
So to do that, we need to kind of put some clamp on this value here.
Okay, so we can put track of speed minus equals one and then we can put if.
Track a spade in this case becomes less than because it's going into negative.
And what would you say would be an acceptable slow speed for the car?
Well, let's say something like two.
If it gets less than two, because if it's gone into a minus, then obviously it's very close to its
actual car, which means the car is not going very fast at all.
So we'll be happy just having it at two.
So we're just going to set it like that.
Okay.
And then we can take this line and we can do something very similar down here if it gets larger than
15, which is what we're starting with up here.
So if it becomes larger than 15, then we're just going to say, let's just set it at 15.
So these are going to be our sort of threshold values that's going to stop those values going behind
the cars.
Okay, so save that.
Let's go back into unity and we will press play and have a look how that helps us a little bit with
this.
So the cars are off and they're going to actually a few of them, I think, get through more this time
because their trackers are still at the beginning.
Yeah, we've got three now.
We've got a situation where we have some people who are kind of stuck.
So in this case, I mean, eventually these two you can see on the screen are fighting it out.
Eventually they're actually going to get around to a point where they can continue this guy here.
He's not even stuck against anybody.
He's just hit the tyre so he can't move.
So what we need to do is come up with some other kind of avoidance code that's going to help in the
situation where these cars are stuck.
Now, these other cars that are coming around the corner may help unstick these cars just by the fact
that they're going to possibly drive into them if they don't avoid them.
But anyway, let's have a look at how we can actually unstick these cars.
So let's stop pressing play.
Okay.
Now, for the next bit to work, we need to put some barriers on our track and you'll see I have haphazardly
added some barriers.
Now this barrier model is called barrier wall and if you just drag and drop that into your project,
I've put it inside of my race track folder just there.
So bring one of those in to the scene.
And what you need to do with it is add a box collider and I'll tell you why in a moment when we start
coding.
But the thing about this Box Collider is it needs to go all around here, okay?
So you need to duplicate it and put it around the outside.
Now also make sure that it's on the inside of where that track boundary was or is so that when the cars
hit into the side, they actually hit this barrier.
And you'll need to do that there and up the other end around there.
Right now, once you've done that, what we're going to do is to program the cars to actually reverse
if they are in trouble, as in they've hit something in the front and they can't sort of go any further.
They've basically stopped.
So what we're going to do is put that code into our void.
Scripts.
So open up your avoid detector script.
And the first thing we're going to do up the top is to put in a bool value for reversing.
So public float reverse equals false.
Okay.
Now other than that, we also need to get the rigid body because we want to check the speed of the vehicle.
And we're basically going to say that if the speed is a certain speed or very slow, basically nothing,
then we're going to reverse.
Okay, let me make this a bool right now.
We will set reverse to be false if we've exited a trigger point.
So reverse equals false.
And we're also going to allow reverse to work not only on cars, but also if we've hit other parts of
the world.
Now, the reason why I got you to put those barriers with box colliders on it was because the box colliders
work on a trigger stay, the trigger stay will not trigger.
If the car runs into that barrier, you need to have that box closed.
And now the same thing is going to happen when we run into these tyres.
Currently they have mesh colliders on them.
If you want them to become objects that if a star car gets stuck on it will reverse, then you'll need
to change them from mesh colliders into something more simple, like a cylinder, or you can put a box
around them as well.
So just keep that in mind.
Okay.
So what are we going to do with the code, first of all, is we're going to have a specific condition,
is that if it is a car, get rid of the return and we'll put all of this inside of brackets.
Okay.
Now up the top here.
What we want to do is determine where the collision happened.
So if the car ran in to the back of another car, then it's going to do its avoidance thing.
But the car in front, if it get hit, then it can just keep going forward.
So we're basically concerned about if we've hit something from behind, that's when we want to do the
avoidance, because currently if both cars hit each other, then you're going to get the car in front.
Also doing some avoidance stuff which can actually get it to turn around and do odd things that we don't
want.
How do we figure out whether a car is in front of another one?
Let me show you.
So we first of all, figure out the vector to the car that we've just hit so we can do that with a collision.
Ah, and it's going to be a this dot transform dot inverse transform point collider dot game object
dot transform dot position.
So we're going to get the vector from the car to what it hit with the car as the origin point.
So basically if the vector is facing upwards and therefore it has a sort of I guess a positive X and
Z direction, then it must have hit something that was in front of it rather than something that was
behind it.
So we can use that as a condition in here.
So we're going to go.
If Collision Diagnostics is greater than zero and Collision Direct Z is greater than zero, then the
thing that we have here is in front of us, okay?
And therefore we can then do our collision avoidance stuff.
So let me put that inside of there.
Now, the other thing we want to do in here is do the reverse.
So if the rigid body dot velocity dot magnitude is, let's say, less than one, that like we're barely
moving, then we will go reverse equals true.
Now we're doing this outside of testing for the car because we really don't care what we've hit.
We just want to reverse anyway.
So this will then become a else just here.
Okay, great.
So we now have reverse being turned on and being turned off.
It needs to be addressed inside of our eye controller code.
Now, just before we finish up with this code, the other thing we need to do is assign this rigid body
and we need a start in that case.
So we want a void start and then we can put rigid body equals this.
Don't get component rigid body.
Okay, so save that.
Now, you probably see that I've got this reverse script going on up here.
Don't worry about that.
That was just me testing something.
You do not need it.
Let's go into our controller and we're going to come in to our update.
Okay, so we've already hooked up a void so that we can use a void.
Now, what are we going to do here with our values for our acceleration and our steering is actually
reverse them.
Okay.
So if you think about it, if you've hit something and you want to reverse, you're going to reverse
in the opposite direction to which you actually want to end up going forward in.
And of course, acceleration is the other way around.
So we can really quickly put a little if statement here and go if a void reverse then.
We can say that the acceleration is going to be whatever it is multiplied by negative one, and then
our steering will equal the same thing, negative one times a.
Just there and not a it'll be.
S So that's just reversing it and it's only going to reverse it around if we're doing a particular reverse.
Okay, so save that.
Now let's go back into Unity and have a look.
Now, all of these changes are actually going to make your cars not get stuck as much as they were before.
So it might take a few laps before we actually see some of those in action.
Okay.
So this happened quicker than I thought.
Let's see if this car does a reverse.
Yes, he does.
So he just ducked away from there.
He might have to do another one.
Well, that one started, too.
And they are doing reverses to get out of their predicament, as you can see.
Great.
So this is going to much improve them getting around the track and then getting out of trouble.
Now cars can also implement different steering mechanics that really relate to flocking.
We're going to be looking at flocking in a later section in this course.
So feel free to then come back here and go, how can I implement flocking into my cars, which you can,
and then you probably do without a lot of the extra collision physics that we've been putting in here.
But the idea here was to show you how the vehicle physics in unity works.
So I hope you enjoyed that.
Please play around with it.
There's so many different things and different logic you can put in here for these cars to get them
working.
But this is definitely a good start for your learning in Car I mechanics.