WEBVTT

0
00:13.230 --> 00:19.210
In this lecture, I'd like you to follow along as we examine moving along a straight line using vectors.

1
00:19.270 --> 00:25.480
So download the resource project that comes with this lecture. In there you'll find a Man/character

2
00:25.660 --> 00:31.870
and on top of that is being added a Move script and it's the Move script that we're going to be working

3
00:31.900 --> 00:32.200
in.

4
00:32.200 --> 00:34.670
So open that up and have a look in here.

5
00:34.870 --> 00:39.020
You can see in the script that there is a Vector3 which is a goal.

6
00:39.340 --> 00:47.170
So the Vector3 is a 3D version of our vector which has an x, y and z and we're initially setting

7
00:47.170 --> 00:55.120
that vector to be 5 in the x-direction, 0 in the y-direction and 4 in the z-direction. Because

8
00:55.120 --> 00:58.060
this is a humanoid character on a flat surface,

9
00:58.060 --> 01:04.960
when we move him around, we're not adding any 'y' because you don't want him to go up and float in the

10
01:05.050 --> 01:05.660
air.

11
01:05.950 --> 01:09.940
If it was an airplane or something that flies it would be completely different.

12
01:09.940 --> 01:13.290
Notice also there's no physics whatsoever involved in this project.

13
01:13.300 --> 01:19.710
We're moving this character purely using our own code and that's so that we can keep control of it.

14
01:19.780 --> 01:28.130
Now in the Start function, we're adding our Vector3 onto the current position of our character.

15
01:28.330 --> 01:32.160
So here we've got a transform and we're running a Translate.

16
01:32.170 --> 01:39.190
What that does is it adds the 'goal vector' to the 'transform vector'. With that saved, go back to

17
01:39.190 --> 01:41.020
Unity and press Play.

18
01:41.350 --> 01:47.710
And what you'll see is your character jump 'that much distance' along that vector. Because it's in the

19
01:47.710 --> 01:48.630
Start function,

20
01:48.630 --> 01:54.960
it's adding the whole lot at once and moving your character to that goal.

21
01:54.970 --> 01:56.780
Now that's not going to work

22
01:56.800 --> 02:03.490
if you want to move the character bit-by-bit in the Update and make it appear as though it's moving

23
02:03.490 --> 02:11.140
itself. So stop playing. Let's go back into Move and have a look at moving this Translate down

24
02:11.200 --> 02:13.540
into the Update.

25
02:13.540 --> 02:19.590
Get rid of the one from the Start and save that, switch back into Unity and press Play.

26
02:21.280 --> 02:26.470
And what you'll notice is the character completely disappears, but if you select the Man in the Hierarchy,

27
02:26.950 --> 02:33.010
have a look at the transform and the position in the x and the z are going up and up and up and that's

28
02:33.010 --> 02:38.500
because that whole vector is being added each time an update occurs.

29
02:38.500 --> 02:44.500
So he is moving, he's just jumping in very big leaps and getting further and further away so that

30
02:44.500 --> 02:48.210
we can't see him. So let's stop that.

31
02:48.220 --> 02:56.500
What we want, instead of jumping to the goal position, is to move in little tiny steps along here, in exactly

32
02:56.500 --> 02:59.470
the same direction as that vector.

33
02:59.710 --> 03:07.390
So to get a smaller version of the vector, you can divide the larger vector by a number that will give be something

34
03:07.410 --> 03:07.750
littler.

35
03:07.750 --> 03:15.890
So if we just go back into Move and in the Start, let's go

36
03:15.890 --> 03:28.760
goal = goal * 0.5f; and that's going to halve the size of the goal.

37
03:29.090 --> 03:32.450
So now he should move along the same direction,

38
03:32.450 --> 03:38.930
but you should see him stop halfway along and then jump to the goal location.

39
03:38.930 --> 03:45.560
But after that he will keep cycling and adding the goal on. Save that, switch back to Unity, press Play.

40
03:49.270 --> 03:55.030
You probably saw him jump a little bit there and that was him moving in those steps, still a big leap but

41
03:55.060 --> 03:57.040
only half of the vector each time.

42
03:57.040 --> 03:58.890
So let's go back into Move.

43
03:59.050 --> 04:07.340
What happens if we multiply this by 0.01, so we get 10% of that vector instead.

44
04:07.450 --> 04:13.390
Anything you multiply or divide on the vector is going to still be in exactly the same direction that

45
04:13.390 --> 04:15.530
you want your character to go.

46
04:15.760 --> 04:18.280
So let's play this one, have a look.

47
04:20.540 --> 04:25.330
So that's much smoother in the movement and he's moving towards the goal location.

48
04:25.340 --> 04:29.900
Now the goal location itself wasn't a position in space.

49
04:30.050 --> 04:32.090
It was a 'goal direction'.

50
04:32.210 --> 04:38.170
So there's a difference between a point and a vector. A point is a fixed spot somewhere in your world.

51
04:38.860 --> 04:46.470
A vector is just a set of instructions of which direction to move in, from any location in your world.

52
04:46.480 --> 04:53.260
So your character is now somewhere, if you click on him, still moving along at 10% of 'goal' each

53
04:53.260 --> 04:54.820
time.

54
04:54.820 --> 05:01.620
Now if we simply multiply 'our vector we want our character to move along' by a small number, then

55
05:01.660 --> 05:07.870
whenever that vector changes, it's going to affect our characters speed.

56
05:07.870 --> 05:13.840
He won't be moving at a constant speed and we can illustrate that by adding in another character.

57
05:13.840 --> 05:23.110
So first of all let's make our vector goal public, so that we can change it, then we will still multiply

58
05:23.170 --> 05:27.300
it by 0.01 and translate along that.

59
05:27.490 --> 05:28.970
So just save that.

60
05:29.020 --> 05:41.430
Now if we go back into Unity, get the Man and just Ctrl-D to duplicate and then move him out and

61
05:41.430 --> 05:46.950
try and put him moving in this direction, we want them to move parallel to each other.

62
05:46.950 --> 05:48.760
So I just want to put it him there.

63
05:48.930 --> 05:54.740
But for this guy, his move goal, let's multiply it up by 2.

64
05:54.740 --> 05:57.010
So it's twice as far away.

65
05:57.060 --> 05:59.940
It's out here somewhere.

66
06:00.120 --> 06:05.800
So let's make that 10, 0 and

67
06:06.270 --> 06:07.460
8.

68
06:07.530 --> 06:15.580
So now when we run it, we'll see different speeds. see how this guy's going much faster because

69
06:15.580 --> 06:21.820
10% of that longer vector is going to be a bigger jump than 10% of the shorter.

70
06:21.820 --> 06:29.380
So they're not at a uniform speed and that's going to make the movement of your character inconsistent

71
06:29.410 --> 06:34.530
when they're moving along different length vectors.

72
06:34.660 --> 06:37.830
So the question is how can we get a vector

73
06:38.110 --> 06:45.810
that is a standard size, that points in the same direction along the vector that we're traveling along.

74
06:45.970 --> 06:55.690
The solution is to get its 'Normalized' version. The normalized version of any vector is a vector that

75
06:55.690 --> 07:04.120
is 1 in length and you can find the normalized version of a vector by dividing each of the x, y and

76
07:04.130 --> 07:09.520
z values of the vector by the magnitude.

77
07:09.520 --> 07:12.530
Now you can also get Unity to do it for you.

78
07:12.730 --> 07:21.160
Let's go back into the Move and instead of having our goal here that we're moving along multiplied by

79
07:21.160 --> 07:28.270
0.01 just to make it much smaller, we want to make it into a standard size.

80
07:28.270 --> 07:30.750
So let's just get rid of that altogether

81
07:31.150 --> 07:40.360
and what we're trying to do is translate along goal.normalized...like that and that will be a vector

82
07:40.360 --> 07:45.160
of unit length 1 going in the same direction.

83
07:45.160 --> 07:49.480
Now when you play, this will probably be too far.

84
07:49.600 --> 07:57.840
So if we just come back and run it.  Yes, you can see that they did move at the same rate

85
07:58.040 --> 08:02.490
but it was quite jittery and it was a big hop each update.

86
08:02.690 --> 08:10.430
But we now know that we have a standardized unit 1 movement each update and now that we have this,

87
08:10.430 --> 08:17.500
we can multiply this by a speed value to change how fast our character is going.

88
08:17.510 --> 08:25.570
So let's just create another 'public float speed' and we'll set that (because we want

89
08:25.570 --> 08:38.790
to make it a bit slower) 0.1f and then multiply your normalized goal by your speed...like that

90
08:39.060 --> 08:43.460
and now because it's public, you could change the speed for each of those characters but we'll just leave

91
08:43.460 --> 08:49.420
it 0.1 for both of them because we want to see them moving at the same rate. Press Play...

92
08:50.330 --> 08:56.240
and there you go...and they'll go on for ever and ever

93
08:56.240 --> 09:03.290
because even though we gave them starting vectors that were completely different, they were pointing

94
09:03.290 --> 09:04.380
in the same direction

95
09:04.430 --> 09:07.920
and then they got normalized down to unit 1

96
09:08.090 --> 09:13.950
and so they ended up with exactly the same vector multiplied by exactly the same speed.

97
09:13.970 --> 09:20.180
So it doesn't matter how long the vector is, if you normalize it, you end up with the same little vector

98
09:20.180 --> 09:22.190
which is of unit 1.

99
09:22.190 --> 09:28.430
Now those speeds, even though you think it's a consistent speed, the other factor involved in it (go back

100
09:28.430 --> 09:36.950
into your move code) is the speed at which this Update occurs because every time your game runs an update

101
09:37.340 --> 09:43.060
it's not necessarily at exactly the same time that the last update happened.

102
09:43.070 --> 09:50.540
So you will get inconsistencies in the speed and although you probably won't even notice these, you will

103
09:50.540 --> 09:55.440
see in a lot of code that people multiply by Time.deltaTime

104
09:55.520 --> 10:03.050
and what this does is it smooths out and allows for the difference in the update running.

105
10:03.080 --> 10:09.220
The other thing I should say too before we finish up, is to make this a LateUpdate.

106
10:09.350 --> 10:14.390
Any movement you've got on your characters (and especially when you start to add cameras that

107
10:14.390 --> 10:17.540
follow things around) you'll want it all to happen

108
10:17.540 --> 10:20.780
after all the physics has been calculated in the scene.

109
10:20.780 --> 10:28.040
Currently we don't have physics so it's not an issue, but you should get into the habit of putting your

110
10:28.040 --> 10:34.080
movements into LateUpdate, which I still am not and feel free to correct me throughout,

111
10:34.130 --> 10:41.670
if I do have an update loop with movement in it because I'm bound to, because that's a bad habit of mine.

112
10:41.780 --> 10:51.360
So this should be all we need now to have a nice smooth and consistent movement of our character.

113
10:51.560 --> 10:55.520
Save it. Let's go back into Unity and press Play...

114
10:56.640 --> 10:58.030
...and now...

115
10:58.320 --> 11:00.260
well that speed's quite small, isn't it?

116
11:00.300 --> 11:06.240
You can say that the multiplying of the Time.deltaTime has also slowed it down a fair bit because

117
11:06.630 --> 11:13.560
it's going to be quite a tiny value because of the time difference between update loops and therefore

118
11:13.590 --> 11:19.920
that allows you to then play around with the speeds that you're setting on your characters. So maybe

119
11:19.920 --> 11:27.800
instead of 0.1, you might want to change these to 1 and try it out.

120
11:27.870 --> 11:35.770
That's a bit better but that really is the code you should use when you stop moving a character around.

121
11:35.790 --> 11:42.000
All right so that's it for moving something in a straight line and how to get consistent speed in your

122
11:42.000 --> 11:44.780
character's movements. In the next lecture,

123
11:44.790 --> 11:50.030
we will start to look at moving towards a goal and making our characters turn.