WEBVTT

0
00:09.790 --> 00:12.850
In this lecture, we're going to continue working with waypoint systems

1
00:12.860 --> 00:19.050
but I wanted to show you the more advanced car A.I. that comes with the Unity Standard Assets.

2
00:19.100 --> 00:27.410
So I've started up a project here and added in my red spheres, which I have added to a WPCircuit, which

3
00:27.410 --> 00:33.650
is an empty game object. All the spheres are children. To get the waypoint and the waypoint circuit

4
00:33.650 --> 00:40.420
code and also the car, you need to bring in two imported packages. Up to Assets,

5
00:40.430 --> 00:47.660
click on 'Import Package', the first one you want is 'Utility'. That has the waypoint circuit and the

6
00:47.660 --> 00:53.720
waypoint progress tracker in it - just import the whole thing. The other one that you want to bring

7
00:53.780 --> 00:58.380
in is the Standard Assets > Vehicles.

8
00:58.400 --> 01:02.760
So add those two things to your project.

9
01:02.770 --> 01:10.470
Next, we'll set up our actual circuit, so the script we want to look for is WaypointCircuit.

10
01:10.470 --> 01:11.010
There it is

11
01:11.020 --> 01:16.600
and I'll drag-and-drop that onto the WPCircuit object

12
01:16.600 --> 01:26.390
and then remember that we [Assign using all child objects] like that and it's given me a very nice circuit which

13
01:26.390 --> 01:31.750
is all joined and in order. Remember if yours is out of order, then you can reorder them here to get it to

14
01:31.760 --> 01:36.410
the way you want it or you can physically move them around in the Scene as well.

15
01:36.410 --> 01:43.040
Now the A.I. car that comes with Standard Assets (I've already found it here because I've searched for waypoint)

16
01:43.640 --> 01:46.400
is called CarWaypointBased

17
01:46.430 --> 01:54.680
and you can find that in Vehicles in Standard Assets. If you go down Vehicles > Car > Prefab

18
01:54.700 --> 02:02.020
and it's there as well, so grab that car and bring it into your scene.

19
02:02.150 --> 02:08.330
Now this car here is set up with a lot of complicated things, as far as the sound and the colliders,

20
02:08.360 --> 02:15.890
so we probably don't want to shrink it or enlarge it to suit our circuit, instead we should change

21
02:16.000 --> 02:17.480
our circuit and make it a bit bigger.

22
02:17.480 --> 02:19.220
So I'll just quickly do that.

23
02:21.940 --> 02:29.230
The car itself is grey and you might think that the textures are missing from it, but that is the actual

24
02:29.230 --> 02:31.390
color of the car.

25
02:31.450 --> 02:36.540
If you want to change that, you'll have to find the material and then put something else on top of it.

26
02:36.820 --> 02:42.560
But for now we'll just stick with the default. Later on I'll show you how to use another car with this

27
02:42.560 --> 02:46.190
same system, not just the Unity car.

28
02:46.630 --> 02:49.450
Right, so select the car in the Hierarchy.

29
02:49.450 --> 02:54.830
Have a look in the Inspector and scroll down and you'll find the Waypoint Progress Tracker

30
02:54.940 --> 02:56.290
already attached to it,

31
02:56.290 --> 02:58.920
so you don't need to add a new one.

32
02:58.990 --> 03:04.990
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

33
03:04.990 --> 03:07.560
drag-and-drop that down into there.

34
03:07.600 --> 03:11.810
Now the car has its own 'waypoint target' which comes with it.

35
03:11.920 --> 03:16.840
If you click on that, you can find it in

36
03:16.900 --> 03:19.860
Helpers > WaypointTargetObject.

37
03:19.900 --> 03:25.210
This car's quite complex, it's got colliders and lights and all sorts of things.

38
03:25.210 --> 03:33.640
The actual model for the car is under SkyCar and it's the body that has the current shader applied

39
03:33.650 --> 03:34.320
to it.

40
03:34.360 --> 03:43.200
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.

41
03:43.360 --> 03:51.740
At this point we can press Play and have a look at the behavior of the car in the default settings.

42
03:51.830 --> 03:57.920
After a while, it gets a little bit out of control and starts to miss the waypoints

43
03:57.980 --> 04:04.850
and that would be because of its braking habits, its acceleration and all those things that it has programmed

44
04:04.910 --> 04:05.670
into it.

45
04:05.670 --> 04:08.100
You can see here, it's now quite erratic.

46
04:08.360 --> 04:11.920
If we stop and go back and select the car and in the Inspector,

47
04:11.940 --> 04:15.420
scroll down to the Car AI Control script,

48
04:15.500 --> 04:21.990
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

49
04:22.120 --> 04:23.340
to see them a little bit better.

50
04:23.360 --> 04:27.100
So you've got things like Cautious Speed Factor

51
04:27.140 --> 04:35.690
(so how fast do you want the car to go when it's being cautious) and cautious is what we talked about before

52
04:35.690 --> 04:38.300
with braking, you know, when you're coming into a corner,

53
04:38.300 --> 04:40.770
how much you want your car to slow down.

54
04:40.820 --> 04:46.730
There's other stuff like Steer Sensitivity...Acceleration Sensitivity...all these things are defined

55
04:46.790 --> 04:47.710
in the script.

56
04:47.810 --> 04:55.100
If you open up that script and have a look, down here it will tell you in the comments what each of them

57
04:55.130 --> 04:56.480
will do.

58
04:58.500 --> 05:00.720
But we're not going to go through all of that.

59
05:00.750 --> 05:02.070
You can read through it yourself.

60
05:02.160 --> 05:10.650
The best way to determine what these do is to Play and watch the behavior as you change them. So let's

61
05:10.650 --> 05:11.930
just press Play...

62
05:14.760 --> 05:17.020
and we'll watch the car as it's going around.

63
05:17.030 --> 05:21.270
So this is the default values that I had before

64
05:21.730 --> 05:27.970
and I will change the Cautious Speed, turn that down.

65
05:28.000 --> 05:35.800
You can see that the car has now slowed, it's much more cautious going into the corners and because there's

66
05:35.800 --> 05:38.110
a lot of waypoints for it to hit,

67
05:38.350 --> 05:45.550
it's constantly braking and slowing down. You can also change the maximum distance that you want to be cautious

68
05:45.610 --> 05:45.940
at.

69
05:45.940 --> 05:53.170
So we could probably put that down to 10, which will allow it to speed up more in between it and

70
05:53.170 --> 05:56.590
only brake quite hard when it gets closer to a corner.

71
05:56.590 --> 05:59.220
So you can see the different behavior that you got there.

72
05:59.230 --> 06:04.180
Now you can spend a lot of time on your own playing around with all of these values to see what they

73
06:04.180 --> 06:04.730
do

74
06:04.930 --> 06:12.100
and you can add multiple cars like we did before, just by doing a Ctrl-D on this car and then

75
06:12.490 --> 06:19.090
getting another copy of it and pressing play and you'll have two cars. In the code, if you're interested

76
06:19.090 --> 06:23.200
to have a look, it has avoidance of other cars as well,

77
06:23.200 --> 06:28.400
even though the physics system will stop them from bumping into each other.

78
06:28.400 --> 06:32.440
So that's our purple car, let me delete that.

79
06:32.450 --> 06:39.160
What we're going to do is use our own car model and your own car needs to be set up the right way.

80
06:39.320 --> 06:47.570
The car itself (if we have a closer look at it) the wheels are separate objects to the cars

81
06:47.570 --> 06:55.020
body and the script allows them to turn and they have different colliders on them as well.

82
06:55.160 --> 07:01.520
So if you bring your own model in, it needs to have separate wheels and they also need to be facing the

83
07:01.520 --> 07:02.090
same way.

84
07:02.090 --> 07:05.540
So have a look at the axis system on this particular wheel.

85
07:05.690 --> 07:10.850
The forward vector for the wheels needs to be pointing in that direction,

86
07:11.030 --> 07:17.900
otherwise, you will end up with strange wheels that rotate around the wrong way, which might be the look

87
07:17.900 --> 07:18.850
you are after.

88
07:18.980 --> 07:20.960
But if you want a normal car, it has to be like that.

89
07:20.960 --> 07:27.170
Now we can get a quick example of a car that fits our needs for this in the Asset Store

90
07:27.170 --> 07:33.680
and this is the little free orange car that I use mostly for my examples.

91
07:33.680 --> 07:38.450
If you read through the description, you'll see that it says it has separated wheels so you can animate

92
07:38.450 --> 07:41.820
them with the vehicle simulation system, which is what we're using.

93
07:41.930 --> 07:47.810
So let's just grab that car from there. You can also bring it in from the other projects that we've created

94
07:47.810 --> 07:49.190
as well.

95
07:49.190 --> 07:56.060
Now when that car finally comes in, in the Scene, we want to drag-and-drop that car in and put it over

96
07:56.060 --> 07:59.330
the top of the existing vehicle.

97
07:59.360 --> 08:03.520
So let me find it...CartoonCarFree > Prefabs

98
08:03.520 --> 08:04.400
There it is.

99
08:04.460 --> 08:08.970
Let's bring it in and put it with out other car.

100
08:09.110 --> 08:11.450
Now it needs to be facing in the same direction,

101
08:11.450 --> 08:14.870
so let me rotate it around.

102
08:15.040 --> 08:21.940
We want to position it as a child of the CarWaypointBased object (that's the currently the purple

103
08:21.940 --> 08:28.330
car) because it's got all of the scripts and colliders and we're just going to basically reuse them all.

104
08:28.340 --> 08:35.770
So grab hold of your FreeCar and drag-and-drop it and make it a child of the CarWaypointBased object.

105
08:35.770 --> 08:40.510
So you've now got the SkyCar and the FreeCar. With the free car

106
08:40.510 --> 08:47.450
there, you want to position it at 0,0,0 relative to the purple car...like that

107
08:47.560 --> 08:55.720
and we probably want to resize it at this point because it's a bit small, so let's just hit the R-key,

108
08:55.750 --> 09:00.150
select our car and just make it a little bit bigger.

109
09:00.450 --> 09:06.100
I'm looking for the wheels to be about the same size.

110
09:06.120 --> 09:06.470
All right.

111
09:06.480 --> 09:10.160
So from there I'm just also going to move it up a little bit.

112
09:12.380 --> 09:13.710
Like that...ok,

113
09:13.820 --> 09:16.380
not too bad.

114
09:16.460 --> 09:25.760
Now the next thing that you need to do is to set the wheels up. The wheels are the driving force of the car.

115
09:25.820 --> 09:33.290
The first thing we're going to do is align the wheel colliders and the wheel hubs with the wheels of the

116
09:33.440 --> 09:34.650
orange car.

117
09:35.000 --> 09:41.840
So we go up to the parent object the CarWaypointBased and have a look down through the Inspector and

118
09:41.840 --> 09:47.430
you're looking for the wheel colliders, here and the wheel meshes.

119
09:47.450 --> 09:53.580
So these are the colliders of each wheel on the purple car.

120
09:53.780 --> 09:56.880
The wheel meshes are the actual wheels themselves

121
09:56.900 --> 10:00.560
and this is the reason why they need to be separate objects.

122
10:00.560 --> 10:07.190
Now these the SkyCars wheels. We're going to replace those with our FreeCar wheels. So, drop down the free

123
10:07.190 --> 10:08.170
car here

124
10:08.480 --> 10:14.030
and what you want to do is grab each wheel and drag it in to the correct position here.

125
10:14.030 --> 10:17.210
So the first one it wants is the Front

126
10:17.210 --> 10:22.190
Right wheel, so it's a matter of finding which one is the Front Right wheel.

127
10:22.190 --> 10:31.960
In fact, I might just rename these...then when you've got them, go back to your parent object and you want

128
10:31.960 --> 10:34.180
to drag-and-drop all of these into the right spot.

129
10:34.180 --> 10:36.520
So the first one is the Front

130
10:36.760 --> 10:37.860
Right

131
10:37.960 --> 10:45.850
and then you've got the Front Left and then you've got the Rear Right and the

132
10:48.630 --> 10:50.100
Rear Left.

133
10:50.290 --> 10:50.570
Right.

134
10:50.580 --> 10:58.950
At this point, if we Play, the car isn't going anywhere but not for want of trying. If we go over

135
10:59.100 --> 11:02.830
to the Scene view, we can have a look at what's going on.

136
11:02.940 --> 11:05.400
So let's just turn this around and zoom in.

137
11:05.400 --> 11:15.480
So we've got 2 cars, the wheels for the orange car are now sitting over the top of the purple car wheels, which

138
11:15.480 --> 11:22.890
is where we want them to be.  So this vehicle system will move those wheel models of the orange car because

139
11:22.890 --> 11:25.110
we have assigned them.

140
11:25.140 --> 11:33.480
Now the issue of why this isn't going anywhere is because the orange car has a box collider on it and that

141
11:33.480 --> 11:38.000
box collider (if we can get in close enough) is on the ground.

142
11:38.010 --> 11:45.170
So it's dropped down onto the ground and it's basically stopping the car from going anywhere.

143
11:45.380 --> 11:47.380
It's unable to push it forward.

144
11:47.430 --> 11:53.730
So the easy fix for this is to select your FreeCar and untick the Box Collider

145
11:53.730 --> 11:56.500
or you could remove it entirely.

146
11:56.550 --> 11:58.940
Let's Play and have a look at this.

147
11:58.980 --> 12:07.030
Now the orange car is going to drive and it's being driven by its own wheels on the purple car.

148
12:07.080 --> 12:15.150
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

149
12:15.150 --> 12:20.230
cars wheels very well because of the purple car, so select the SkyCar

150
12:20.310 --> 12:23.370
and just untick it, turn it off so we can't see it.

151
12:23.730 --> 12:32.290
As I said before, the wheels of the orange car are now sitting in the locations of the purple car and

152
12:32.290 --> 12:39.300
we need to move those so that they sit naturally within the body of the cars model.

153
12:39.640 --> 12:46.290
That's where these wheel hubs (that are part of the cars system) come into play

154
12:46.300 --> 12:53.350
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

155
12:53.350 --> 12:55.010
can find out where it is).

156
12:55.030 --> 13:00.540
This is the Front Right and it's positioned near the front right of the tire...

157
13:00.820 --> 13:04.780
but it's positioned near the front right of the tire of the PURPLE car.

158
13:04.780 --> 13:10.410
We want to move it so that it's positioned on the front right of the ORANGE car.

159
13:10.690 --> 13:16.900
The SkyCar itself is pretty superfluous at this point because we're not going to use it, so I'm just going

160
13:16.900 --> 13:17.820
to turn that off.

161
13:17.820 --> 13:19.570
You could delete it.

162
13:19.780 --> 13:21.930
Let's go back to our WheelHubs

163
13:21.940 --> 13:27.340
(this is our Front Right) and have a look at where it is. So it's positioned there.

164
13:27.340 --> 13:39.290
We need to grab hold of it and move it, so that it sits nicely over our existing orange car tire.

165
13:39.480 --> 13:47.340
You want to do that with each of these hubs, so I'll quickly do that...

166
13:47.360 --> 13:56.800
All right so with that done, we can now press Play and just pause it again to quickly find our car...and

167
13:56.800 --> 14:05.130
you can see that the wheels haven't moved away from the body of the orange car.

168
14:05.300 --> 14:14.710
Finally, just for the physics system, if we grab hold of our car again, we're going to go into the Colliders

169
14:14.710 --> 14:19.030
section, which is under the CarWaypointBased...the first one at the very top.

170
14:19.030 --> 14:26.350
Inside you've got a ColliderBody, a ColliderBottom and a ColliderFront and they're all set up

171
14:26.350 --> 14:32.620
based on the geometry of the purple car. If you want to fit them nicely over your orange car which will give

172
14:32.620 --> 14:36.940
you more realistic collisions with this one, then you want to just modify them.

173
14:37.090 --> 14:44.250
So this is the body of the car which is currently...there.

174
14:44.320 --> 14:46.650
So let's just make that a bit bigger...

175
14:50.230 --> 14:53.120
and move it up.

176
14:53.300 --> 14:58.010
Then there's the front collider, which is on an angle that you can see there.

177
14:58.100 --> 14:59.360
It's not too bad.

178
14:59.420 --> 15:04.920
I might just move it up a little bit...and you can adjust that as you like.

179
15:04.920 --> 15:11.440
Then there's the bottom skirt, which you can see there, which is quite low for the orange car.

180
15:11.460 --> 15:17.870
Again you can move it up, so that it sits at the bottom...

181
15:18.720 --> 15:23.710
like that and it probably needs to be just a tad shorter as well.

182
15:24.950 --> 15:26.020
There.

183
15:26.090 --> 15:26.500
All right.

184
15:26.510 --> 15:35.930
So that is all of the setting up that you need to do for your own custom car in the vehicle simulation

185
15:35.930 --> 15:37.760
system that comes with unity.