WEBVTT

0
00:10.770 --> 00:16.800
We begin this lecture where we ended in the previous one, with our orange car that is following our

1
00:16.800 --> 00:20.730
tracker around the track and also braking and accelerating.

2
00:20.760 --> 00:28.770
Now we want to add in an opponent, so let's go up into the Hierarchy, grab hold of the orange car and

3
00:28.770 --> 00:32.670
then Ctrl-D to create another one.

4
00:32.670 --> 00:37.210
Let's just change the name of that and call it BlueCar

5
00:37.380 --> 00:44.860
and then in the Scene, if you hit the W-key, you should be able to drag that extra car out...like that.

6
00:44.910 --> 00:46.680
Now this is our blue car.

7
00:46.890 --> 00:48.270
It's not very blue.

8
00:48.330 --> 00:54.330
So let's create another material for it. Now if you have a look down in the Assets, there's

9
00:54.330 --> 00:57.540
'CartoonCarFree > models' where the car model is

10
00:57.540 --> 01:00.270
and then there's a material.

11
01:00.270 --> 01:06.570
This material here, the CartoonVehicleAtlas, just Ctrl-D on that, which will give you a second

12
01:06.570 --> 01:07.020
one.

13
01:07.170 --> 01:14.910
Rename that to BlueCar, so we know which one it is, then with it selected, over in the Inspector you

14
01:14.910 --> 01:19.470
want to set the main color to blue...

15
01:23.240 --> 01:24.600
something like that.

16
01:24.740 --> 01:25.000
Right.

17
01:25.000 --> 01:33.210
So with that new blue, you can drag that over and just drop it onto the car. It's probably not dark enough.

18
01:33.210 --> 01:36.230
It's just made the windows a bit 'purplish'.

19
01:36.300 --> 01:43.090
Now you can change it with the car in the scene. It's because the orange is already in the texture

20
01:43.090 --> 01:46.380
that it's going to be a bit odd. If you want to make it a true blue,

21
01:46.390 --> 01:48.910
then you can take this texture

22
01:48.910 --> 01:54.780
here, open it up in Photoshop, make the orange parts blue and then put it back onto the material.

23
01:55.210 --> 01:56.980
But for now let's go with this.

24
01:57.130 --> 01:57.370
Right.

25
01:57.370 --> 02:03.720
So you've got the car separated, you've got exactly the same thing on the orange car and the blue car.

26
02:03.730 --> 02:05.380
All you've done is make a copy.

27
02:05.380 --> 02:08.870
Let's press Play and have a look at the behavior.

28
02:09.040 --> 02:14.200
So the orange car (as it always has) is taking off...and off it goes.

29
02:14.410 --> 02:18.420
The blue car has stalled completely.

30
02:18.450 --> 02:24.130
If we switch over to the Scene, we can see that it's just sitting there looking at the cube and the cube's

31
02:24.130 --> 02:27.520
not going anywhere.

32
02:27.540 --> 02:31.890
Now, the problem with this is...well there's two problems.

33
02:31.890 --> 02:38.490
First of all, the car isn't accelerating. It doesn't believe that it needs to accelerate and therefore

34
02:38.520 --> 02:45.750
it's starting value of 0 for its speed is still set to 0.

35
02:45.750 --> 02:51.660
Remember back when we first started working with these waypoints in the little tank and the tank

36
02:51.750 --> 02:58.350
would be still...and stuck to the waypoint and wouldn't be going anywhere unless you dragged the tank after

37
02:58.680 --> 03:00.590
its circuit tracker.

38
03:00.690 --> 03:05.430
If I grab hold of the blue car, let's just see if I can get this to get going...there we go.

39
03:05.430 --> 03:11.790
So now that's working because it added some speed in your calculations to the movement of the actual

40
03:11.790 --> 03:12.760
car.

41
03:12.990 --> 03:18.990
Now, what's happening? This is the problem that's happened...let's stop this. Press Play and then switch back

42
03:18.990 --> 03:31.640
to the Scene view. Initially, our code is saying if we have a big angle between our car and the forward facing

43
03:31.640 --> 03:36.010
vector of our cube, then you're going to put the brakes on.

44
03:36.050 --> 03:38.540
If we have a look in detail of what we've got here,

45
03:38.540 --> 03:43.790
there's our forward-facing vector for our car. If we click the cube, there's the forward-facing vector

46
03:43.790 --> 03:44.640
of the cube.

47
03:44.810 --> 03:52.280
So right from the very beginning, we've hit that condition that says put on the brakes because the angle

48
03:52.280 --> 03:55.630
is too great between the car and the tracker.

49
03:56.240 --> 04:02.200
To fix this, we go back into the code and where we're testing whether we should be braking or not,

50
04:02.240 --> 04:08.360
we'll also test if we're moving at a certain speed because if we're not moving fast enough, there's no

51
04:08.360 --> 04:15.740
need to brake. So we'll put an '&& speed > 10'.

52
04:15.740 --> 04:18.570
Save that and go back and run

53
04:18.860 --> 04:22.430
and let's see if we can get our little blue car to move.

54
04:23.410 --> 04:23.690
Yes.

55
04:23.750 --> 04:24.350
So there we go.

56
04:24.350 --> 04:28.220
So both of them are now moving. If we go back into the scene,

57
04:29.420 --> 04:31.470
we should be able to see them come around...here

58
04:31.680 --> 04:33.540
they are. They're on top of each other.

59
04:33.650 --> 04:40.940
They have collider but they don't have a rigid body, so therefore they are not going to cause collisions

60
04:41.270 --> 04:42.470
with each other.

61
04:43.960 --> 04:45.570
So that's easy enough to fix.

62
04:45.580 --> 04:48.400
Select your orange car. In the Inspector,

63
04:48.400 --> 04:54.400
you want to [Add Component] and add a Rigidbody and then you want to do the same thing with the blue

64
04:54.400 --> 05:02.650
car. [Add Component], Rigidbody. To see exactly what's going on, you might want to go in and select the

65
05:02.650 --> 05:09.570
cubes that are our trackers and turn off their mesh renderers, so they don't get in the way of the two

66
05:09.580 --> 05:10.170
cars.

67
05:10.180 --> 05:10.540
Right.

68
05:10.810 --> 05:17.290
Let's press Play and have a look at the behavior now. So you can see that the rigidbody is going to

69
05:17.290 --> 05:22.600
keep those two cars away from each other through the collision system.

70
05:26.180 --> 05:32.270
We can now customize the behavior of each car by exposing all of those variables in the script and then setting

71
05:32.270 --> 05:34.520
them differently in the Inspector.

72
05:34.520 --> 05:41.060
The first thing you want to do though is go to the blue car and down where it's got access to the Readout

73
05:41.060 --> 05:41.490
script,

74
05:41.510 --> 05:48.650
just remove its reference to the Readout, so that the orange car is the only car that's updating

75
05:48.710 --> 05:49.890
the speed readout,

76
05:49.940 --> 05:53.500
otherwise you're going to be getting updated speeds from both cars.

77
05:53.570 --> 06:01.400
Then in the code, you'll need to make sure that before you try and set the readout text, right at the

78
06:01.400 --> 06:08.560
bottom, just put in an 'if(readout)' - if it exists, then put the text in it.

79
06:08.600 --> 06:13.640
Of course, if you don't want to have the 'speed converter' or the 'readout', you can just get rid of the whole

80
06:13.640 --> 06:15.140
system entirely.

81
06:15.140 --> 06:21.380
Now at the top of the program, we can make all of these public, which will give you access to them in

82
06:21.380 --> 06:22.320
the Inspector.

83
06:22.320 --> 06:24.950
So I'll just copy-and-paste that in.

84
06:25.010 --> 06:30.800
The only thing you don't want access to is the speed because that is what is being calculated. So save

85
06:30.800 --> 06:31.580
that.

86
06:31.880 --> 06:38.810
If you switch back into Unity, you'll be able to select either car and change all of these different

87
06:38.810 --> 06:45.440
values. So you can change the acceleration and deceleration amounts, the minimum/maximum speed and also

88
06:45.440 --> 06:48.680
the brake angle as well and see the different behavior.

89
06:48.680 --> 06:52.390
So let's leave the orange car as it was before. With the blue car,

90
06:52.580 --> 07:01.770
let's see if we can increase its acceleration to 8 and it's deceleration to 4

91
07:02.150 --> 07:09.520
and let's see if we can get it to go 120 and we're going to make it quite cautious, so if this brake

92
07:09.520 --> 07:16.530
angle will be 10 (I doubt it's going to get up to those sorts of speeds on this very short track)

93
07:16.540 --> 07:26.500
so let's just save that and run it. Ok, so the blue car goes off, it's got a fantastic acceleration.

94
07:27.890 --> 07:31.310
It's actually doing much better than I thought, I would have thought it would have slowed down a fair bit

95
07:31.310 --> 07:35.810
around the corners and the orange car would have been able to catch up to it, but it looks like the blue

96
07:35.810 --> 07:37.730
car might end up lapping it...

97
07:42.170 --> 07:42.730
Yes, there you go.

98
07:42.770 --> 07:43.630
So yeah.

99
07:43.790 --> 07:47.890
Now at this point you can add a lot of different cars (if you want to) onto your track.

100
07:47.900 --> 07:49.550
You might need to make a bit bigger track.

101
07:49.550 --> 07:55.100
This is only a little one and you've got all these different variables that you can now change to get

102
07:55.100 --> 07:58.170
different behavior out of the cars.