WEBVTT

0
00:11.790 --> 00:18.090
This lecture extends on the last lecture by now moving our characters towards a goal, where they will

1
00:18.090 --> 00:23.050
then stop. To do this, you'll need a goal location.

2
00:23.050 --> 00:30.640
I've created a simple cube by right-clicking, 3D Object > Cube and then positioning it somewhere in

3
00:30.640 --> 00:31.760
the environment.

4
00:32.050 --> 00:36.970
We're going to get one of the characters to move towards the goal, to begin with.

5
00:36.970 --> 00:43.780
So select the characters that are in the scene and make sure that you remove the script that's already

6
00:43.960 --> 00:45.640
attached to them.

7
00:45.640 --> 00:49.540
We're now going to create a new C# file called MoveToGoal.

8
00:49.570 --> 00:53.380
It's a little bit different to the Move code that we wrote before,

9
00:53.710 --> 01:02.320
but we still need to calculate with vectors and at the top begin by adding in a float for speed and

10
01:02.350 --> 01:04.170
I've got that set to 1.0.

11
01:04.290 --> 01:09.120
In fact, I might just set that up to 2 so that things go a little bit faster this time.

12
01:09.340 --> 01:13.490
Then I have a public exposed transform called

13
01:13.490 --> 01:20.520
'goal' which is now changing from the previous code. Before, we had goal as a vector.

14
01:20.710 --> 01:30.430
Now it's a position in the environment. Inside of our LateUpdate, we need to calculate direction.

15
01:30.430 --> 01:38.230
This is the vector between the goal position and the character's position. To do that,

16
01:38.230 --> 01:41.350
we simply minus where we're going

17
01:41.350 --> 01:47.050
(so the position of the goal) minus where we currently are.

18
01:47.590 --> 01:52.150
So this is 'where we're going' and this is 'where we are'.

19
01:52.240 --> 01:53.790
It has to be in that order.

20
01:53.830 --> 01:58.930
If you switch them around the other way, your vector will be facing in the opposite direction

21
01:58.960 --> 02:03.340
and your character will go along the same vector...

22
02:03.380 --> 02:06.580
or at least along the same kind of line towards your target,

23
02:06.710 --> 02:08.890
but it will be traveling in the opposite direction.

24
02:09.020 --> 02:14.940
If you ever get that happen, then you know that you've got these two things around the wrong way.

25
02:15.020 --> 02:21.710
Once you've calculated the new vector to travel along, you then again use your Translate and you

26
02:21.710 --> 02:23.740
put your vector you calculated inside,

27
02:23.810 --> 02:27.080
multiply by speed and multiply by Time.deltaTime.

28
02:27.080 --> 02:29.680
So we're still traveling along a vector

29
02:29.720 --> 02:31.840
but this time we've calculated it.

30
02:31.910 --> 02:34.880
So save that, switch back into Unity.

31
02:34.880 --> 02:41.870
Now you'll want to grab one of your characters and drag-and-drop that script from the project onto them.

32
02:41.930 --> 02:48.350
Here you can see I've got the MoveToGoal script attached and I have set the speed to 2 (well, the code

33
02:48.350 --> 02:49.240
did anyway).

34
02:49.340 --> 02:54.380
Grab hold of your cube and make sure you drag-and-drop that in as the goal position.

35
02:54.380 --> 02:57.860
Now press Play and have a look what happens.

36
02:58.100 --> 03:04.190
So your character has calculated the direction it needs to go and it will get to the goal location and then

37
03:05.090 --> 03:06.440
it kind of stops moving

38
03:06.950 --> 03:14.840
but it's jittering. The reason why is because you're still inside that update loop asking it to

39
03:14.840 --> 03:18.100
calculate the direction to the goal and move towards it.

40
03:19.120 --> 03:28.180
Now in most cases, you're not going to get your character spot on the exact location of your goal and

41
03:28.180 --> 03:34.650
therefore you need to have some kind of 'near enough is good enough' calculation included.

42
03:35.490 --> 03:37.730
Just before we do that, I want to show you something.

43
03:37.750 --> 03:45.000
If we move our character over here now and press Play, it will calculate a new vector to the goal and

44
03:45.000 --> 03:46.890
move towards the goal.

45
03:46.890 --> 03:51.410
Now to stop that jittering that happens when they get to the goal, go back into the code

46
03:51.780 --> 03:57.220
and we want to put something in the update here to test if we've reached our goal.

47
03:57.270 --> 04:03.900
If we have reached our goal, then we don't want to keep calculating the vector towards it and moving

48
04:03.900 --> 04:04.980
towards it.

49
04:05.010 --> 04:12.880
So you can do that by testing the length of the direction. You've already calculated this vector

50
04:13.050 --> 04:17.940
and can you remember what it's called, the length of a vector? It's the Magnitude.

51
04:17.940 --> 04:30.560
So we can say 'if(direction.magnitude > 5)  then move

52
04:30.770 --> 04:32.820
towards the goal.

53
04:32.960 --> 04:36.170
So save that and let's go and check it out.

54
04:36.170 --> 04:42.160
So every time it loops and direction gets calculated, it gets shorter and shorter,

55
04:42.170 --> 04:48.930
the vector itself. It doesn't stay set to the original vector. Even though it's still in the same direction,

56
04:49.220 --> 04:51.510
it will be shorter.

57
04:52.480 --> 04:54.330
So now you can see that it moved.

58
04:54.340 --> 05:00.400
but not very far and that's because a distance of 5 is about that distance

59
05:00.400 --> 05:06.640
in this case, because these are quite small little characters and you'll have to take it upon yourself

60
05:06.910 --> 05:11.240
to check the scaling that's going on within your world.

61
05:11.350 --> 05:18.240
So let's go back here and let's make this 1 and run it again.

62
05:23.540 --> 05:23.790
Ok,

63
05:23.800 --> 05:29.770
so that must be a distance of 1 from the cube and if we click on the cube and have a look at the one

64
05:29.770 --> 05:33.320
I created, you can see that the scale is 0.2.

65
05:33.400 --> 05:37.000
So that makes that cube (in world terms)

66
05:37.000 --> 05:45.840
about 20cm across. When a cube is at size 1 which is its default size, that would be

67
05:45.840 --> 05:48.850
1m x 1m x 1m in the Unity world

68
05:48.850 --> 05:55.570
and therefore you can imagine that 5 of those cubes would fit across that space there, for that distance

69
05:55.570 --> 05:58.670
of 5 from before.

70
05:58.690 --> 06:04.270
Now you can see that one is still quite large, if you want your character to be right on top of the goal

71
06:04.270 --> 06:05.020
location.

72
06:05.020 --> 06:11.470
So let's just try once more with a smaller value. Go back into MoveToGoal and we'll change this to

73
06:11.470 --> 06:14.100
0.2f.

74
06:14.240 --> 06:17.510
Save that and run it again

75
06:18.040 --> 06:22.120
and the character should be very close to the target

76
06:22.180 --> 06:27.080
in this case and you can see that he is, so that's pretty acceptable.

77
06:27.220 --> 06:34.510
I'm going to move this value of 0.1 up the top, so let's make this 'public float accuracy'

78
06:34.840 --> 06:40.540
because it is the accuracy that we're willing to take as far as being close to a goal. Let's set it

79
06:40.540 --> 06:42.930
to 0.01f.

80
06:43.120 --> 06:45.790
So we're going to make it very close

81
06:45.790 --> 06:49.730
and then down in here, we'll put accuracy...like that.

82
06:49.930 --> 06:56.710
All right, so save that and we'll switch back and run it and just see if that makes any difference with

83
06:56.710 --> 06:58.780
that very tight accuracy.

84
06:59.540 --> 07:01.060
That's pretty good.

85
07:01.280 --> 07:07.510
What I want to show you is something that will cause your character to still jitter with this particular

86
07:07.510 --> 07:08.560
code.

87
07:08.980 --> 07:15.500
Let's change the speed to five with that really tight accuracy and let's run it again

88
07:18.940 --> 07:19.210
Ok,

89
07:19.220 --> 07:23.820
so the character jittered before it eventually got within that accuracy.

90
07:24.020 --> 07:30.680
What's going on here is that it got to the goal, it overshot the mark, it recalculates the direction

91
07:30.680 --> 07:40.110
back towards the goal, overshoots the mark and ends up bouncing around like this because your 5 speed

92
07:40.110 --> 07:44.760
here is pushing it over the goal and then pushing it back over the goal and pushing it over the goal

93
07:44.760 --> 07:46.210
like that as well.

94
07:46.440 --> 07:53.100
In this case we were lucky where it did eventually move to a position that it could get within that

95
07:53.100 --> 07:53.690
accuracy

96
07:53.700 --> 07:58.770
but sometimes you won't get in with that accuracy and you'll get some really strange behavior going

97
07:58.800 --> 08:03.310
on as far as the character dancing around the goal spot.

98
08:03.420 --> 08:07.470
That's because it never ever is getting there within your accuracy.

99
08:07.470 --> 08:13.440
So if you get that, just play around with your accuracy values until you've lifted them up high enough

100
08:13.440 --> 08:19.170
to stop that from happening, otherwise you're going to have to reduce the speed at which they are attacking

101
08:19.380 --> 08:21.920
your goal position from.

102
08:22.290 --> 08:25.360
I'm going to put this accuracy back up to 1 for this one

103
08:25.380 --> 08:27.310
and then the other character...

104
08:27.360 --> 08:32.130
let's put the same code onto it and leave it with the default speed,

105
08:32.130 --> 08:37.680
but let's set the accuracy to 0.1 instead of 1.

106
08:37.740 --> 08:43.350
We also need to set the cube into the goal location for this guy here

107
08:43.650 --> 08:50.450
and now if you press Play, they'll move towards the goal position. As we've currently got it working,

108
08:50.450 --> 08:55.460
they will slide along from their current position to the goal position but they don't turn and face

109
08:55.460 --> 08:57.410
the direction that they're traveling in

110
08:57.410 --> 09:01.060
and that would be a nice addition for a character to do that.

111
09:01.960 --> 09:10.150
In the olden days, we had to calculate the angles between vectors and which

112
09:10.150 --> 09:16.000
side the vectors were on, relative to the other vectors, in order to figure out how to turn your character

113
09:16.000 --> 09:16.790
around.

114
09:17.020 --> 09:20.810
Now we've got this great function called LookAt that Unity has

115
09:20.830 --> 09:26.590
which does all that hard work for us and I'm going to put it in the Start function so that our character

116
09:26.590 --> 09:30.480
first turns and looks at where it's about to go.

117
09:30.670 --> 09:37.710
That method, or function is called LookAt and you add it to the end of the transform and then give

118
09:37.710 --> 09:44.080
it the goal position that you want your character to look at and it will snap around and look in that

119
09:44.080 --> 09:45.360
direction.

120
09:45.370 --> 09:54.190
Now at this point, if I run it, because we've changed the orientation of the character this Translate

121
09:54.700 --> 10:00.610
is not going to work properly and we'll just save this and go back and have a quick look at what you

122
10:00.610 --> 10:03.010
get and I'll show you how to fix it.

123
10:03.010 --> 10:09.070
So there we get this and you can see that the characters tend to arc towards it

124
10:09.070 --> 10:14.990
and this character is going to just spiral around until it gets close enough.

125
10:15.040 --> 10:23.560
What's happening there is that now that these characters are no longer (0,0,0) rotation up here in the

126
10:23.560 --> 10:26.280
transform.

127
10:26.290 --> 10:31.600
If you've been following along and you've changed the rotation of your characters, you probably couldn't

128
10:31.600 --> 10:33.450
figure out what was going wrong with your code

129
10:33.460 --> 10:35.560
and this is the exact problem.

130
10:35.560 --> 10:42.340
So in your MoveToGoal, what you want to do in your Translate is 'translate' your character

131
10:42.370 --> 10:49.360
along this vector in 'world space' as opposed to 'local space'.

132
10:49.360 --> 10:56.890
Now if you save that and run, you'll see that your characters turn towards their goal and then they travel

133
10:57.370 --> 11:03.070
towards it. Over in the Scene, I've got these red lines that you would have seen and that's drawing

134
11:03.100 --> 11:08.280
in the vectors from the code with a Debug.DrawRay

135
11:08.410 --> 11:14.300
which draws a ray from the position in a particular direction and then you give it a color.

136
11:14.320 --> 11:17.370
This is really useful for debugging to see what's going on.

137
11:17.380 --> 11:24.730
So I've just included that, so that you can use it in your own code to see which vectors are doing what

138
11:24.730 --> 11:26.770
and which way they're pointing.

139
11:26.770 --> 11:29.810
All right so that's the easy way to do a LookAt.

140
11:30.010 --> 11:37.210
You might have noticed at this point, if we run it again and you grab hold of the cube and in the scene

141
11:37.210 --> 11:43.570
you move it around, you will get the characters responding to its location just naturally, because of

142
11:43.570 --> 11:45.460
what's in that update.

143
11:45.490 --> 11:50.590
Notice though the LookAt isn't updating and that's because it's still in the Start function.

144
11:50.590 --> 11:59.380
So if we change that LookAt here and make sure that we recalculate it every time within our Update,

145
12:00.010 --> 12:08.830
the characters will always be looking at that cube and then what you could do is get some driving script

146
12:08.860 --> 12:16.620
so that you can drive that cube around with the arrow keys and the characters would move wherever it goes.