WEBVTT

00:05.310 --> 00:05.880
Okay.

00:05.880 --> 00:06.660
Welcome back.

00:06.690 --> 00:07.970
We've put the cars in.

00:07.980 --> 00:11.790
We've got the collision and the avoidance stuff on there.

00:11.910 --> 00:19.260
When you pressed play, did you end up with a pile of cars like I'm about to and some weird performance

00:19.260 --> 00:20.120
from things?

00:20.130 --> 00:23.010
So let's have a look at.

00:23.140 --> 00:24.360
Okay, I'm going to pause it.

00:24.360 --> 00:27.480
Some cars have escaped collision.

00:27.510 --> 00:32.550
Some cars are still trying to push their way through and some of them will eventually start going around.

00:32.640 --> 00:34.560
This car is a concern.

00:34.560 --> 00:37.440
Look where the little tracker ended up.

00:37.440 --> 00:40.100
So what on earth happened there?

00:40.110 --> 00:45.750
Well, I can tell you what happened was an oval locus of code that I came up with.

00:45.750 --> 00:51.780
So let's fix that one first with that, because the cars kind of got stuck, right?

00:52.260 --> 01:00.150
The progress tracker ended up being near the car all the time or ahead of the car.

01:00.420 --> 01:06.150
And what happens if these two lines run more than they should?

01:06.150 --> 01:07.140
Pass a certain point.

01:07.140 --> 01:07.610
Okay.

01:07.620 --> 01:10.530
The track of speed could actually go into negative.

01:10.560 --> 01:15.600
Now what that's going to do is place the tracker behind the car.

01:16.290 --> 01:21.870
Or in the other case here, if we're going in the other direction, it could also put it kind of behind

01:21.870 --> 01:23.790
the car if it's sort of going too fast.

01:24.090 --> 01:29.550
So to do that, we need to kind of put some clamp on this value here.

01:29.580 --> 01:36.300
Okay, so we can put track of speed minus equals one and then we can put if.

01:37.940 --> 01:45.150
Track a spade in this case becomes less than because it's going into negative.

01:45.190 --> 01:48.690
And what would you say would be an acceptable slow speed for the car?

01:48.710 --> 01:50.470
Well, let's say something like two.

01:50.480 --> 01:58.850
If it gets less than two, because if it's gone into a minus, then obviously it's very close to its

01:58.850 --> 02:02.120
actual car, which means the car is not going very fast at all.

02:02.120 --> 02:06.470
So we'll be happy just having it at two.

02:07.040 --> 02:08.690
So we're just going to set it like that.

02:08.720 --> 02:09.260
Okay.

02:09.260 --> 02:15.020
And then we can take this line and we can do something very similar down here if it gets larger than

02:15.020 --> 02:17.720
15, which is what we're starting with up here.

02:17.720 --> 02:22.820
So if it becomes larger than 15, then we're just going to say, let's just set it at 15.

02:22.820 --> 02:29.750
So these are going to be our sort of threshold values that's going to stop those values going behind

02:29.750 --> 02:30.480
the cars.

02:30.550 --> 02:32.030
Okay, so save that.

02:32.150 --> 02:41.360
Let's go back into unity and we will press play and have a look how that helps us a little bit with

02:41.360 --> 02:41.810
this.

02:41.810 --> 02:51.170
So the cars are off and they're going to actually a few of them, I think, get through more this time

02:51.170 --> 02:53.000
because their trackers are still at the beginning.

02:53.210 --> 02:54.470
Yeah, we've got three now.

02:54.470 --> 02:59.210
We've got a situation where we have some people who are kind of stuck.

02:59.420 --> 03:05.720
So in this case, I mean, eventually these two you can see on the screen are fighting it out.

03:05.720 --> 03:10.910
Eventually they're actually going to get around to a point where they can continue this guy here.

03:10.910 --> 03:13.040
He's not even stuck against anybody.

03:13.040 --> 03:16.460
He's just hit the tyre so he can't move.

03:16.520 --> 03:21.680
So what we need to do is come up with some other kind of avoidance code that's going to help in the

03:21.680 --> 03:23.660
situation where these cars are stuck.

03:23.660 --> 03:29.570
Now, these other cars that are coming around the corner may help unstick these cars just by the fact

03:29.570 --> 03:35.060
that they're going to possibly drive into them if they don't avoid them.

03:35.060 --> 03:40.040
But anyway, let's have a look at how we can actually unstick these cars.

03:40.040 --> 03:42.650
So let's stop pressing play.

03:43.340 --> 03:43.760
Okay.

03:43.760 --> 03:52.940
Now, for the next bit to work, we need to put some barriers on our track and you'll see I have haphazardly

03:52.940 --> 03:54.290
added some barriers.

03:54.290 --> 04:03.080
Now this barrier model is called barrier wall and if you just drag and drop that into your project,

04:03.080 --> 04:08.120
I've put it inside of my race track folder just there.

04:08.180 --> 04:12.470
So bring one of those in to the scene.

04:12.470 --> 04:19.220
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

04:19.220 --> 04:20.000
coding.

04:20.000 --> 04:26.940
But the thing about this Box Collider is it needs to go all around here, okay?

04:26.990 --> 04:31.640
So you need to duplicate it and put it around the outside.

04:31.640 --> 04:42.050
Now also make sure that it's on the inside of where that track boundary was or is so that when the cars

04:42.050 --> 04:45.500
hit into the side, they actually hit this barrier.

04:45.710 --> 04:51.020
And you'll need to do that there and up the other end around there.

04:51.110 --> 05:00.050
Right now, once you've done that, what we're going to do is to program the cars to actually reverse

05:00.050 --> 05:06.620
if they are in trouble, as in they've hit something in the front and they can't sort of go any further.

05:06.620 --> 05:08.150
They've basically stopped.

05:08.150 --> 05:13.940
So what we're going to do is put that code into our void.

05:14.710 --> 05:15.330
Scripts.

05:15.330 --> 05:19.810
So open up your avoid detector script.

05:19.810 --> 05:25.450
And the first thing we're going to do up the top is to put in a bool value for reversing.

05:25.450 --> 05:31.870
So public float reverse equals false.

05:32.440 --> 05:33.060
Okay.

05:33.070 --> 05:41.680
Now other than that, we also need to get the rigid body because we want to check the speed of the vehicle.

05:41.680 --> 05:50.140
And we're basically going to say that if the speed is a certain speed or very slow, basically nothing,

05:50.140 --> 05:51.370
then we're going to reverse.

05:51.440 --> 05:55.540
Okay, let me make this a bool right now.

05:55.540 --> 06:00.460
We will set reverse to be false if we've exited a trigger point.

06:00.460 --> 06:04.180
So reverse equals false.

06:04.630 --> 06:13.690
And we're also going to allow reverse to work not only on cars, but also if we've hit other parts of

06:13.690 --> 06:14.350
the world.

06:14.380 --> 06:21.940
Now, the reason why I got you to put those barriers with box colliders on it was because the box colliders

06:21.940 --> 06:29.500
work on a trigger stay, the trigger stay will not trigger.

06:29.530 --> 06:33.980
If the car runs into that barrier, you need to have that box closed.

06:33.980 --> 06:37.330
And now the same thing is going to happen when we run into these tyres.

06:37.330 --> 06:39.430
Currently they have mesh colliders on them.

06:39.430 --> 06:46.180
If you want them to become objects that if a star car gets stuck on it will reverse, then you'll need

06:46.180 --> 06:53.230
to change them from mesh colliders into something more simple, like a cylinder, or you can put a box

06:53.230 --> 06:54.520
around them as well.

06:54.520 --> 06:56.290
So just keep that in mind.

06:56.320 --> 06:56.740
Okay.

06:56.740 --> 07:02.530
So what are we going to do with the code, first of all, is we're going to have a specific condition,

07:02.530 --> 07:12.820
is that if it is a car, get rid of the return and we'll put all of this inside of brackets.

07:13.120 --> 07:13.510
Okay.

07:13.510 --> 07:15.640
Now up the top here.

07:15.640 --> 07:20.350
What we want to do is determine where the collision happened.

07:20.350 --> 07:28.690
So if the car ran in to the back of another car, then it's going to do its avoidance thing.

07:28.960 --> 07:33.790
But the car in front, if it get hit, then it can just keep going forward.

07:33.790 --> 07:39.580
So we're basically concerned about if we've hit something from behind, that's when we want to do the

07:39.580 --> 07:46.630
avoidance, because currently if both cars hit each other, then you're going to get the car in front.

07:46.630 --> 07:51.700
Also doing some avoidance stuff which can actually get it to turn around and do odd things that we don't

07:51.700 --> 07:52.240
want.

07:52.600 --> 07:55.630
How do we figure out whether a car is in front of another one?

07:55.630 --> 07:56.260
Let me show you.

07:56.260 --> 08:02.800
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.

08:03.700 --> 08:15.730
Ah, and it's going to be a this dot transform dot inverse transform point collider dot game object

08:15.730 --> 08:18.940
dot transform dot position.

08:18.940 --> 08:30.040
So we're going to get the vector from the car to what it hit with the car as the origin point.

08:30.040 --> 08:39.160
So basically if the vector is facing upwards and therefore it has a sort of I guess a positive X and

08:39.190 --> 08:44.860
Z direction, then it must have hit something that was in front of it rather than something that was

08:44.860 --> 08:45.850
behind it.

08:45.850 --> 08:49.240
So we can use that as a condition in here.

08:49.240 --> 08:49.840
So we're going to go.

08:49.840 --> 09:03.520
If Collision Diagnostics is greater than zero and Collision Direct Z is greater than zero, then the

09:03.520 --> 09:06.700
thing that we have here is in front of us, okay?

09:06.700 --> 09:12.460
And therefore we can then do our collision avoidance stuff.

09:12.460 --> 09:14.460
So let me put that inside of there.

09:14.470 --> 09:17.890
Now, the other thing we want to do in here is do the reverse.

09:17.890 --> 09:27.790
So if the rigid body dot velocity dot magnitude is, let's say, less than one, that like we're barely

09:27.790 --> 09:31.810
moving, then we will go reverse equals true.

09:31.810 --> 09:36.250
Now we're doing this outside of testing for the car because we really don't care what we've hit.

09:36.280 --> 09:38.440
We just want to reverse anyway.

09:38.440 --> 09:42.820
So this will then become a else just here.

09:43.960 --> 09:44.680
Okay, great.

09:44.680 --> 09:50.410
So we now have reverse being turned on and being turned off.

09:50.410 --> 09:55.850
It needs to be addressed inside of our eye controller code.

09:55.870 --> 10:00.580
Now, just before we finish up with this code, the other thing we need to do is assign this rigid body

10:00.580 --> 10:02.560
and we need a start in that case.

10:02.560 --> 10:10.090
So we want a void start and then we can put rigid body equals this.

10:10.890 --> 10:13.920
Don't get component rigid body.

10:15.150 --> 10:17.130
Okay, so save that.

10:17.370 --> 10:20.430
Now, you probably see that I've got this reverse script going on up here.

10:20.460 --> 10:21.450
Don't worry about that.

10:21.450 --> 10:22.950
That was just me testing something.

10:22.950 --> 10:24.240
You do not need it.

10:24.360 --> 10:32.370
Let's go into our controller and we're going to come in to our update.

10:32.680 --> 10:36.960
Okay, so we've already hooked up a void so that we can use a void.

10:36.990 --> 10:43.410
Now, what are we going to do here with our values for our acceleration and our steering is actually

10:43.410 --> 10:44.440
reverse them.

10:44.460 --> 10:44.940
Okay.

10:44.940 --> 10:50.280
So if you think about it, if you've hit something and you want to reverse, you're going to reverse

10:50.280 --> 10:55.610
in the opposite direction to which you actually want to end up going forward in.

10:55.620 --> 10:57.900
And of course, acceleration is the other way around.

10:57.900 --> 11:06.450
So we can really quickly put a little if statement here and go if a void reverse then.

11:07.140 --> 11:14.190
We can say that the acceleration is going to be whatever it is multiplied by negative one, and then

11:14.190 --> 11:18.660
our steering will equal the same thing, negative one times a.

11:19.390 --> 11:21.810
Just there and not a it'll be.

11:21.820 --> 11:28.900
S So that's just reversing it and it's only going to reverse it around if we're doing a particular reverse.

11:28.960 --> 11:30.430
Okay, so save that.

11:30.460 --> 11:34.270
Now let's go back into Unity and have a look.

11:34.270 --> 11:40.950
Now, all of these changes are actually going to make your cars not get stuck as much as they were before.

11:40.960 --> 11:44.770
So it might take a few laps before we actually see some of those in action.

11:47.650 --> 11:48.070
Okay.

11:48.070 --> 11:49.930
So this happened quicker than I thought.

11:49.960 --> 11:52.960
Let's see if this car does a reverse.

11:53.980 --> 11:54.790
Yes, he does.

11:54.790 --> 11:57.040
So he just ducked away from there.

11:57.040 --> 11:58.390
He might have to do another one.

11:58.670 --> 12:00.190
Well, that one started, too.

12:00.190 --> 12:07.030
And they are doing reverses to get out of their predicament, as you can see.

12:07.060 --> 12:07.810
Great.

12:07.810 --> 12:15.730
So this is going to much improve them getting around the track and then getting out of trouble.

12:18.360 --> 12:24.740
Now cars can also implement different steering mechanics that really relate to flocking.

12:24.750 --> 12:28.320
We're going to be looking at flocking in a later section in this course.

12:28.320 --> 12:35.280
So feel free to then come back here and go, how can I implement flocking into my cars, which you can,

12:35.280 --> 12:39.930
and then you probably do without a lot of the extra collision physics that we've been putting in here.

12:39.930 --> 12:44.790
But the idea here was to show you how the vehicle physics in unity works.

12:44.790 --> 12:46.920
So I hope you enjoyed that.

12:46.920 --> 12:48.180
Please play around with it.

12:48.180 --> 12:52.530
There's so many different things and different logic you can put in here for these cars to get them

12:52.530 --> 12:52.950
working.

12:52.950 --> 13:00.270
But this is definitely a good start for your learning in Car I mechanics.
