WEBVTT

00:05.410 --> 00:11.200
In this lecture, we're going to add a little bit of extra code to make the cars turn away from each

00:11.200 --> 00:12.640
other if there's a collision.

00:12.640 --> 00:17.100
And this code will help cars pass each other as well.

00:17.110 --> 00:24.760
So what we're going to do is take the capsule point or our tracker point that we're trying to drive

00:24.760 --> 00:29.650
to and move it by a certain offset if we detect a collision.

00:29.800 --> 00:33.190
So let's take the car that's behind in this case.

00:33.190 --> 00:41.110
When it runs into the car in front of us, it's going to trigger a collision in both cars.

00:41.110 --> 00:52.120
Script on both cars is going to recalculate a new goal position that is based on the tracker position,

00:52.120 --> 00:54.610
plus a bit of an offset.

00:54.760 --> 01:00.730
And what it's going to do is going to make one car turn in one direction and the other car turn in the

01:00.730 --> 01:08.260
other direction, which will give you some really interesting avoidance dynamics with relatively simple

01:08.260 --> 01:09.010
code.

01:09.010 --> 01:14.380
And what we're going to do is use the colliders and the rigid body that's already on the car.

01:15.500 --> 01:16.010
Okay.

01:16.010 --> 01:18.350
So let's add in the code.

01:18.350 --> 01:25.370
To do this, you'll need a script called a void detector, and you can see how it's spilt over here.

01:25.370 --> 01:29.000
So it's just adding a C-sharp script to your project.

01:29.000 --> 01:32.870
Let's open that up and add in the code.

01:32.870 --> 01:37.850
So at the top, we're going to add in a public.

01:38.890 --> 01:39.880
Float.

01:40.720 --> 01:42.520
Avoid path.

01:42.760 --> 01:43.810
Equals zero.

01:44.200 --> 01:46.300
Public float.

01:46.690 --> 01:48.220
Avoid time.

01:48.460 --> 01:49.690
Equals zero.

01:49.690 --> 01:53.350
And these are settings that we are going to use from elsewhere.

01:53.350 --> 01:56.320
But you can also set in the inspector.

01:56.530 --> 02:04.390
Then we want a public float for wander distance equals zero.

02:04.690 --> 02:09.150
And that's going to be how far we can actually wander away from the path.

02:09.160 --> 02:10.480
Actually, let's not make that zero.

02:10.480 --> 02:21.250
Let's make that for and public float for a void length, which is how long we want to keep avoiding.

02:21.250 --> 02:21.700
Four.

02:21.700 --> 02:26.950
So this is the time that we want to go off on our avoiding path.

02:26.950 --> 02:31.060
Now we're not going to use a start and update in these.

02:31.060 --> 02:35.410
We're going to use trigger values to determine when we should move.

02:35.410 --> 02:47.680
So we want void on trigger exit collider call and we're going to basically figure out when two cars

02:47.680 --> 02:54.550
have come within triggering distance of each other and then use that to actually put some avoiding code

02:54.550 --> 02:55.030
in.

02:55.300 --> 03:08.500
So collect game object tag does not equal car then we will turn and if it does, then we'll go avoid

03:08.500 --> 03:11.020
time equals zero.

03:11.260 --> 03:18.130
So we're actually setting the avoid time back to zero if we're exiting a sort of a collision or being

03:18.130 --> 03:19.390
in the road of someone.

03:19.390 --> 03:24.460
Now the actual code is going to happen for avoiding when we're in the trigger distance.

03:24.460 --> 03:34.570
So on trigger stay collider call and let's put that other E out of there.

03:34.600 --> 03:34.840
Okay.

03:34.900 --> 03:40.330
And then we're going to go if called game object tag.

03:40.570 --> 03:41.830
It's not a car.

03:41.860 --> 03:47.740
Of course we're checking that we've got a tag of car so that we're only checking we collide with cars.

03:47.740 --> 03:53.680
In order for this script to work, for example, we don't want other things to sort of trigger.

03:53.680 --> 03:58.720
For example, there's any other triggers that you might put into your environment and then we'll go

03:58.720 --> 04:15.550
rigid body for the other car equals called get component, rigid body avoid time equals time time which

04:15.550 --> 04:19.720
is the time now plus the avoid length.

04:21.610 --> 04:28.270
Then we will create a vector three, which will be the other car local target.

04:28.270 --> 04:36.640
So where the other car is, with respect to the current car, it's going to equal transform inverse

04:36.640 --> 04:38.980
transform point.

04:39.970 --> 04:43.120
Again, it needs to be relative not in world coordinates.

04:43.390 --> 04:44.320
Other car.

04:44.320 --> 04:46.310
Dot game object.

04:46.330 --> 04:48.520
Dot transform.

04:48.520 --> 04:50.110
Dot position.

04:50.710 --> 04:51.940
Float.

04:51.940 --> 04:53.770
Other car angle.

04:53.770 --> 04:56.200
The angle to the other car of course.

04:56.200 --> 05:01.120
So this will be our math f dot a10 two.

05:01.150 --> 05:08.530
Same angle code we've used for moving around our waypoints and calculating those other.

05:09.480 --> 05:10.380
CA.

05:11.310 --> 05:23.880
Local target and it'll be dot x and then other local target dot z.

05:24.750 --> 05:27.840
So same as our progress tracker, our waypoints.

05:27.840 --> 05:32.490
When we were looking at the angle of which way, which direction we were going and where it is now,

05:32.970 --> 05:35.580
we're doing that with the cars.

05:35.610 --> 05:35.980
Okay.

05:36.000 --> 05:46.440
And then we're going to set our void path equals wander distance multiplied by negative math f sine

05:46.870 --> 05:48.900
other car angle.

05:48.900 --> 05:54.480
So we're actually taking the negative sine of the angle to the other car so that we turn by the same

05:54.480 --> 06:01.170
amount of angle in the other direction, so away from the other car.

06:01.200 --> 06:03.330
Okay, so that's all we need in here.

06:03.330 --> 06:07.620
We're going to save this and we're going to go back into unity.

06:07.620 --> 06:13.170
We're going to select our car because we need to put this avoid script onto it.

06:13.590 --> 06:14.040
Okay.

06:14.040 --> 06:18.240
And we're going to grab and put a void detector onto there.

06:18.660 --> 06:18.840
Okay.

06:18.960 --> 06:20.820
So here's our values here.

06:21.210 --> 06:26.280
The wander distance and the avoid length of the things that you can change and play around with.

06:26.280 --> 06:32.970
The other things are actually used inside our controller script, so open up that.

06:32.970 --> 06:40.290
And what we're going to do is at the top and you might have seen these in the previous video as lines

06:40.290 --> 06:43.140
of code that weren't necessary yet, this one here.

06:43.140 --> 06:49.200
So we want to get a hold of our voice detector script on this particular car and when to keep a copy

06:49.200 --> 06:51.750
of it so that we're not having to get it all the time.

06:51.750 --> 07:00.690
And then inside of our start, we will actually set it and we'll get the avoid detector that is on this

07:00.690 --> 07:02.280
particular game object.

07:02.430 --> 07:02.850
All right.

07:02.850 --> 07:06.900
So we're going to now use this down inside of our update.

07:06.900 --> 07:12.480
So if we come down into the update, you'll see the code already in here.

07:12.480 --> 07:15.330
So I've moved things around just a little bit.

07:15.330 --> 07:20.850
All I've done here at the top is a vector three local target is no longer set up here.

07:20.880 --> 07:24.420
It gets set based on the avoidance time.

07:24.480 --> 07:31.590
If there is no avoidance time needing to happen, then we've got the value that we had before.

07:32.070 --> 07:42.570
Otherwise we're going to create a different local target which includes our avoid path using the trackers

07:42.570 --> 07:44.220
right direction.

07:44.220 --> 07:48.180
So sideways movement multiplied by the avoid path.

07:48.180 --> 07:50.940
Or it actually just does what the diagram at the beginning of this lecture does.

07:50.940 --> 07:56.640
It just moves the tracker one way or the other, depending on where we want to go next.

07:56.820 --> 07:57.300
Okay.

07:57.300 --> 08:07.140
So then the target angle which we are calculating up the top, I've also moved that down into here below

08:07.170 --> 08:15.330
this code because the local target is what's used for calculating this target angle.

08:15.330 --> 08:21.300
Okay, now this distance to target, we took out in the previous lecture that I had still hanging around

08:21.300 --> 08:21.840
in there.

08:21.960 --> 08:23.340
You don't need it.

08:23.340 --> 08:25.560
Obviously we're not using it for anything.

08:25.590 --> 08:28.230
Of course you could comment it out at this moment.

08:28.230 --> 08:35.190
You might want to use it later on, put it back as another one of your variables, but it's not necessary.

08:35.310 --> 08:37.170
Okay, so this is the changes.

08:37.170 --> 08:38.940
Let's save this now.

08:39.510 --> 08:41.820
We'll go back into unity.

08:41.820 --> 08:49.110
Now our car needs to have a trigger on it so that it can actually trigger other cars if you select it.

08:49.110 --> 08:57.720
At the moment, it's got a collider on it, which is very nice and very tightly bound to the car.

08:58.290 --> 09:04.890
You don't want to use this because by the time this collider and another car's version of this collider

09:04.890 --> 09:07.830
have hit each other, cars have actually hit each other.

09:07.830 --> 09:13.470
So we want a bit of a buffer sitting around this car, and we don't want that buffer to actually be

09:13.470 --> 09:18.630
a physics object as far as a rigid body and or cause sort of physical collisions.

09:18.630 --> 09:20.130
We just want to as a trigger.

09:20.310 --> 09:27.360
So select the car and then over in your inspector, we're going to go down the bottom here and we're

09:27.360 --> 09:31.650
going to add a box collider so do that.

09:31.860 --> 09:38.580
Then in the scene we need to find where that goes and it's going to go like down here.

09:38.900 --> 09:39.040
Okay.

09:39.180 --> 09:42.210
So it sits basically on the origin of the car.

09:42.870 --> 09:45.870
You have to align this up with the car basically.

09:45.870 --> 09:47.760
So let's move it up.

09:47.790 --> 09:52.560
It only sort of really needs to be around the body and wherever other cars are going to hit it.

09:52.770 --> 09:53.970
So let's have a look.

09:53.970 --> 09:59.280
We're going to move the center of this box collider up into the center of the car.

09:59.460 --> 10:05.220
Then the size of it we can grab the Z value and if you start dragging that, eventually it will pop

10:05.220 --> 10:07.530
out both ends of the actual car.

10:07.530 --> 10:10.740
And you can see that coming through now there for me.

10:11.240 --> 10:11.540
Okay.

10:11.570 --> 10:19.370
Now, the fact that it's no higher than this wintergreen value is kind of okay for me because no other

10:19.370 --> 10:22.130
car is going to sort of hit it up there.

10:22.430 --> 10:27.500
Now, how close do you want other cars to be able to get to cars before they decide that they're going

10:27.500 --> 10:28.460
to swerve out of the way?

10:28.490 --> 10:33.230
Well, that will depend on what your Z value is here, how much of a room around it.

10:33.350 --> 10:39.320
The other one you'll want is probably a bit of side buffering as well, so that they don't get to close

10:39.320 --> 10:40.070
together.

10:40.430 --> 10:44.300
And that's another thing you can kind of play around with if you want.

10:44.450 --> 10:52.220
Now, once you've got that, you're going to go over and set is trigger on that box.

10:52.220 --> 10:58.070
Collider So that it will use those trigger methods that we just coded in.

10:58.100 --> 11:04.180
Finally, you want to go up to the inspector at the top and you want to tag your car as car.

11:04.190 --> 11:10.430
If there is no car in your list, you want to go to add tag add in the tag car.

11:10.430 --> 11:12.520
We used lowercase car.

11:12.530 --> 11:19.850
Make sure you do that, then go back to the car and make sure you select car for its tag.

11:20.090 --> 11:22.100
Okay, well, we've done it right.

11:22.100 --> 11:22.610
That's it.

11:22.610 --> 11:24.690
So that is one car.

11:24.710 --> 11:29.330
Now, at this point, it's probably a good idea to save your project.

11:29.360 --> 11:38.510
I keep having issues with mine crashing, which I do believe is my video, my screen capture software.

11:38.510 --> 11:40.670
But it does cause inconveniences.

11:40.790 --> 11:41.180
Right.

11:41.180 --> 11:43.160
So let's add another car.

11:43.160 --> 11:46.600
So select that car control dx that car.

11:46.610 --> 11:52.790
And in the scene, we're going to hit the key and move that other car out.

11:53.340 --> 11:54.320
Okay, so there it is.

11:54.320 --> 12:00.470
There now this other car is an exact copy of our car over here, which means it's going to have the

12:00.470 --> 12:02.210
camera attached to it.

12:02.210 --> 12:07.250
So make sure you go in that second car and remove the camera because you only really want the camera

12:07.250 --> 12:08.090
on this one.

12:08.300 --> 12:11.300
Or you could take it off altogether and not have it on any car.

12:11.300 --> 12:13.100
It depends what you want to do with this.

12:13.100 --> 12:19.760
So there's one car and let's control D this second car because it doesn't have the camera and then we

12:19.760 --> 12:24.620
can use the W to move that over here.

12:24.770 --> 12:26.150
Control D.

12:27.390 --> 12:34.170
And you can add as many cars as you like at this point.

12:34.170 --> 12:36.720
You can press play and have a look at what you get.

12:36.750 --> 12:38.950
I will tell you, it's not going to be perfect.

12:38.970 --> 12:43.650
In fact, we're going to come back in the next lecture and look at evaluating what we've done.
