WEBVTT

0
00:11.150 --> 00:17.300
In this lecture, we will create the foundation of a simple racing car driven by A.I. So far, we've developed

1
00:17.360 --> 00:21.200
an NPC that can follow a waypoint circuit progresser around at a constant

2
00:21.200 --> 00:21.790
rate,

3
00:21.950 --> 00:24.920
but in reality, that's not how cars move.

4
00:24.920 --> 00:31.700
Instead, we need to consider the cars velocity, acceleration and deceleration and program the NPC to

5
00:31.700 --> 00:34.040
know when to speed up and when to slow down.

6
00:35.830 --> 00:41.610
Velocity is the vector representing the NPC speed, as well as the direction it is traveling.

7
00:41.740 --> 00:49.250
The speed is used to drive the NPC forward. Acceleration and deceleration are considered forces that can

8
00:49.250 --> 00:52.770
increase or reduce the velocity over time.

9
00:52.880 --> 00:59.790
They are also vectors. Usually acceleration is a forward force and deceleration is a backward force, though

10
00:59.810 --> 01:00.790
they could be applied at

11
01:00.800 --> 01:05.510
any angle. For example, in the case of a collision from the side.

12
01:05.510 --> 01:10.550
So far we've developed an NPC that will follow a point that is tracking around a waypoint circuit.

13
01:10.550 --> 01:14.930
This point we will use as the NPCs eyes for how the track is turning.

14
01:15.200 --> 01:20.410
When the track is running straight, the NPC and the tracker will have the same forward vector.

15
01:20.510 --> 01:26.330
However, when the circuit is bending, the angle between the tracker and the NPCs forward vectors will get larger.

16
01:26.330 --> 01:36.320
To follow along, as we put together this simple car A.I., download the WaypointsCarsStarter resources

17
01:36.360 --> 01:37.080
with this.

18
01:37.390 --> 01:42.100
When you open it, you'll get exactly what I'm looking at here, though your Scene and Game views might

19
01:42.100 --> 01:48.470
be slightly different, but in it you've got a racing track with a waypoint circuit already set

20
01:48.550 --> 01:52.430
up on it, using the same code that we used in the last lecture

21
01:52.690 --> 01:58.690
and I've added in an orange car. That orange car has a cube attached to it.

22
01:58.690 --> 02:03.580
Now this orange car has the WaypointProgressTracker set up on it.

23
02:03.790 --> 02:09.660
The cube is set as the Target for that WaypointProgressTracker 

24
02:09.670 --> 02:18.250
and there's a C# script called SimpleAICar attached to that car. That script, if we open it up, you'll

25
02:18.250 --> 02:24.760
find is very similar to the last lot of code that we've got for pushing objects along in a straight

26
02:24.760 --> 02:27.880
line, whilst they're continually looking at a goal.

27
02:27.910 --> 02:35.250
So we're going to take this script and modify it to add a little bit of car dynamics to it.

28
02:35.530 --> 02:43.210
You may also have seen, back in this scene in the game that there's a speedometer that's been added.

29
02:43.210 --> 02:49.570
This is driven by code called AnalogSpeedConverter and we're not going to go into that

30
02:49.570 --> 02:54.160
but the speed from the car is being fed into the speedometer.

31
02:54.220 --> 02:59.740
So it gives you a visual representation of what the car is doing with respect to its speed.

32
02:59.740 --> 03:06.680
Now with this car, if we just press Play then you'll see it go around the track at a constant speed.

33
03:06.790 --> 03:12.960
So it's going to go up to 200 (as you can see in the dial) and just constantly follow around the cube

34
03:12.970 --> 03:15.580
whether you're in the corner or not.

35
03:15.580 --> 03:20.590
This speed isn't very indicative of an actual speed, so it says it's going at 200

36
03:20.590 --> 03:24.300
but is this 200 'meters per second' or 200 'kilometers an hour'.

37
03:24.430 --> 03:26.820
It doesn't look like it's traveling at any of those speeds

38
03:26.830 --> 03:29.110
and that's because of the way that we're calculating it.

39
03:29.260 --> 03:33.410
So we'll change that as well and update it, so the dial makes sense.

40
03:33.440 --> 03:38.930
Back in our code, the first thing we'll do is to modify the way that the speed is set up.

41
03:38.950 --> 03:45.360
So I want you to get rid of these two lines (or move them about) and we're going to put in this.

42
03:45.370 --> 03:51.700
So we start with an acceleration of 5, then a deceleration which I've got as 5 as well (these two

43
03:51.700 --> 03:59.500
values you can change, based on how much power and braking power the car is going to have), a minimum

44
03:59.500 --> 04:01.210
speed and a maximum speed

45
04:01.220 --> 04:07.690
(because without this, you can get your car going backwards when you don't want it to because it's braked

46
04:07.690 --> 04:13.480
too much), and a max speed to stop it from going way too fast to infinity.

47
04:13.480 --> 04:19.570
Speed itself comes down here, now I've taken the public off of this because speed is not an exposed variable

48
04:19.570 --> 04:19.920
any more.

49
04:19.920 --> 04:21.300
We're going to calculate it.

50
04:21.310 --> 04:23.140
We're not going to set it.

51
04:23.210 --> 04:29.980
The other things I've got here are acceleration, deceleration, min and max speed NOT as exposed variables,

52
04:29.990 --> 04:32.490
though you can expose them later.

53
04:32.500 --> 04:35.210
So they are to be set by the user.

54
04:35.260 --> 04:40.360
The reason I do this is because I always forget when I'm editing that if I try and change the values

55
04:40.360 --> 04:44.210
here and they're exposed, it doesn't change in the Inspector

56
04:44.220 --> 04:47.920
and then I spend a long time trying to figure out why my code's not working.

57
04:48.100 --> 04:53.290
So I find it better not to have them exposed until I've got everything working the way I want.

58
04:53.290 --> 04:55.990
Next we'll add our calculation for the speed.

59
04:56.090 --> 05:02.500
Come down to just before the speed is used, in this Translate function here and above that, we're going to

60
05:02.620 --> 05:10.810
add in THIS: 'Speed ='. We're using a Mathf.Clamp and that's going to make sure that whatever calculation

61
05:10.810 --> 05:17.480
for speed we put in here, will get clamped between the minSpeed and the maxSpeed.

62
05:17.650 --> 05:24.610
So this bit first in the clamp is doing a calculation for us, which it will then put into 'speed'.

63
05:24.640 --> 05:31.180
If however it falls outside of that range, it will clamp it either to 0 or whatever the maxSpeed

64
05:31.330 --> 05:32.130
is.

65
05:32.200 --> 05:42.490
This calculation is increasing the speed with...acceleration * Time.deltaTime

66
05:42.490 --> 05:49.060
and because we've multiplied by Time.deltaTime up there, we can come down and remove it out of

67
05:49.060 --> 05:50.110
the Translate

68
05:50.110 --> 05:53.770
so they were only doing a simple speed.

69
05:53.770 --> 06:00.550
Now the other thing with this speed converter (which is showing you the dial on the screen) is it takes

70
06:00.550 --> 06:04.840
a 'min' and 'max' value to work out how much it should rotate the needle.

71
06:04.960 --> 06:12.850
Now, in this case, this will be minSpeed and the 100 will be the maxSpeed.

72
06:12.910 --> 06:16.180
So it's going to go, in this case, between 0 and 100.

73
06:16.180 --> 06:20.920
So let's save that, go back to Unity and press Play

74
06:21.070 --> 06:27.690
and now instead of the dial zipping around and hitting the top speed, it will slowly accelerate.

75
06:27.730 --> 06:32.560
So you can see the dial increasing and the car following the cube. You can also see that we need to fix

76
06:32.560 --> 06:35.050
the formatting of our speed

77
06:35.320 --> 06:41.230
and then we get to a point that you can see here. See how the car's jumping round and round doing circles

78
06:41.230 --> 06:48.400
around that tracker? It's the same problem that we've had in the past with our NPCs when they're not quite reaching

79
06:48.400 --> 06:50.570
the point that they need to be at

80
06:50.680 --> 06:57.190
and this car essentially can't follow the tracker because it's stuck going around it. Now to

81
06:57.200 --> 07:06.300
fix that, if you go into your car and go down to your WaypointProgressTracker attached circuit, the

82
07:06.300 --> 07:08.960
'Look Ahead For Target' will be set to 1

83
07:09.010 --> 07:13.220
and also the 'Look Ahead For Speed', which are just too big.

84
07:13.270 --> 07:17.320
Turn them down to 0.1 instead,

85
07:17.320 --> 07:20.450
so that we're not looking so far ahead.

86
07:20.560 --> 07:24.170
We're staying much closer to that waypoint tracker.

87
07:24.190 --> 07:30.930
Then if we run again, you'll see that's staying nice and close

88
07:30.940 --> 07:36.600
but what it's not going to do is get too far away and it's not going to cut corners either.

89
07:36.760 --> 07:44.890
So as you increase that value for the 'Look Ahead Target', you'll start to get your car further and further

90
07:44.890 --> 07:51.460
away from the waypoint tracker. If I just pause this for a second...because it's calculating in a straight line

91
07:51.490 --> 07:56.410
to that point, when the waypoint tracker is around the corner up here

92
07:56.590 --> 08:01.600
and if the cars over here, it could track straight across the track here and you will find that as you build

93
08:01.990 --> 08:09.670
more systems that you will get the car lagging behind the cube and it will cut corners and not stay

94
08:09.670 --> 08:16.990
on the track perfectly and that's why you need to adjust these 'Look Ahead Target' and speed values to

95
08:16.990 --> 08:21.120
your certain circumstance and your certain tracks. So, if we go back to play...

96
08:21.160 --> 08:29.650
we're now going at 100, which is extremely fast as you can see on this track, but the car is following

97
08:29.740 --> 08:36.430
the circuit tracker and following the track, so that's good.

98
08:36.450 --> 08:39.660
Now what's happening though is, it's getting up to 100

99
08:39.720 --> 08:43.290
and that's its constant speed. Of course, real cars don't do that.

100
08:43.290 --> 08:45.420
You don't go round the corner at 100.

101
08:45.420 --> 08:52.660
So we now want to change it so that we'll brake when we're coming up to a corner. If you can run and pause

102
08:52.770 --> 08:55.860
in a corner, you'll get a situation like this.

103
08:55.860 --> 09:03.300
Notice how the cars forward-facing vector is not aligned with the forward facing vector of the tracker.

104
09:03.360 --> 09:08.200
Whenever this happens, it means that the car is traveling in a different direction to the tracker

105
09:08.490 --> 09:14.490
and therefore we know that the tracker has gone around a corner and the car hasn't.

106
09:14.490 --> 09:21.060
When this happens we want to apply the brakes (or bring in deceleration into our speed calculation) and

107
09:21.060 --> 09:27.300
we can determine when to do this in our code by calculating the angle between each forward vector. Back

108
09:27.300 --> 09:28.100
in the code,

109
09:28.100 --> 09:35.220
we'll add in another variable at the top: 'float brakeAngle' and this will

110
09:35.220 --> 09:41.370
be the angle at which we will apply the brakes, so because it's a variable, you can change it for different

111
09:41.370 --> 09:46.020
car types. Some can be very cautious and brake when there's a small angle.

112
09:46.050 --> 09:52.530
Others can be a bit more daredevil and you can have a large angle, but let's set 20 to start with as

113
09:52.590 --> 09:54.080
our angle.

114
09:54.240 --> 10:02.280
So that's out brake angle that will test the angle between our goal forward vector and our car forward

115
10:02.340 --> 10:02.970
vector.

116
10:03.150 --> 10:03.950
So we'll do that

117
10:03.960 --> 10:11.450
down here in the Update. Currently, we're calculating the speed all by itself just here

118
10:11.460 --> 10:19.110
and it's only got the acceleration. Now deceleration is going to be the same deal, but it will be a minus.

119
10:19.140 --> 10:21.470
Let's replace this line here

120
10:23.020 --> 10:32.260
with this. So this calculates with a Vector3.Angle. You can give it two vectors so the forward vector

121
10:32.260 --> 10:35.990
of the goal and the forward vector of the car

122
10:36.340 --> 10:40.880
and then test if that angle is greater than the brake angle.

123
10:40.960 --> 10:47.630
If it is, then we're going to decelerate (so we're going to apply the brakes essentially),

124
10:47.740 --> 10:51.330
otherwise we will keep accelerating. While we're in here,

125
10:51.370 --> 10:58.390
we'll fix that read out text on the speedometer, by casting speed to an 'int', just to cut off all the

126
10:58.390 --> 10:59.890
decimal places.

127
10:59.890 --> 11:00.170
Right,

128
11:00.160 --> 11:00.810
save that.

129
11:00.820 --> 11:02.510
Now let's go and play.

130
11:02.590 --> 11:08.440
So we begin by accelerating and when we get to the first corner you'll see the speed have a little hiccup

131
11:08.710 --> 11:14.380
and then as it goes around other corners, you'll see the needle of the speed dip just down a little bit,

132
11:14.380 --> 11:15.470
by a little bit.

133
11:15.520 --> 11:21.160
But the thing is, it's still accelerating and if we leave it to run long enough, it will get up to maximum

134
11:21.160 --> 11:22.330
speed.

135
11:22.330 --> 11:29.860
Now the reason that that is occurring is because the rotation between the cars forward vector and the cubes

136
11:29.890 --> 11:32.010
forward vector isn't

137
11:32.050 --> 11:36.080
different enough for us to worry about applying the brakes.

138
11:36.130 --> 11:43.290
The reason that is, if we stop playing, go into the car and down to your WaypointProgressTracker.

139
11:43.510 --> 11:50.410
If you have a look, you've got this 'Look Ahead For Speed' value which is currently set at 0.1.

140
11:50.410 --> 12:00.760
Now that's the cube rotating itself, for however far ahead it can look on the track and how much it can

141
12:00.760 --> 12:04.380
rotate. So, this is like the cubes rotational speed.

142
12:04.390 --> 12:13.240
Now if we change that to something like 1, it will allow the cube to update a lot quicker for further

143
12:13.240 --> 12:20.010
ahead. Now if you watch the cube as we press Play here, you'll see it turning on its own axis

144
12:20.110 --> 12:25.360
and what that will do is then allow the car to apply the brakes into the corners. So as it comes to the

145
12:25.360 --> 12:32.000
corner, see that cube rotates and flips around quite quickly (and up here again it will do it as well)

146
12:32.040 --> 12:39.520
and the brakes are now being applied a lot because there is that big difference between the car and

147
12:39.520 --> 12:44.470
the cube and you can see that it's really putting a dampener on the speed here.

148
12:44.470 --> 12:51.110
So again it comes down to "What are you after, in this?" and "How do you want it to look and work?".

149
12:51.130 --> 12:57.730
It will depend on the size of your world and also the size of the track as well because the world...remember

150
12:57.730 --> 13:01.120
that a cube is 1 meter x 1 meter x 1 meter

151
13:01.340 --> 13:06.620
and therefore this track (you can see that white cube was made as a unit cube),

152
13:06.630 --> 13:07.970
it's not a very big track.

153
13:07.990 --> 13:13.690
You can't expect to get up to a great speed. If you want real-world speeds, then you need to make sure your

154
13:13.690 --> 13:17.250
track is in the real-world size coordinates as well.

155
13:17.410 --> 13:22.420
So let's leave this here and in the next lecture we'll start to look at customizing the properties to

156
13:22.420 --> 13:24.520
make unique NPC behavior.