WEBVTT

0
00:11.500 --> 00:14.400
Besides turning a character and moving it along a set vector,

1
00:14.440 --> 00:20.230
we can also turn the character and push it in the direction it is facing (or along one of its local vectors).

2
00:20.980 --> 00:21.650
In Unity,

3
00:21.670 --> 00:29.950
a character has 3 local vectors: z for forward, y for up and x for right, regardless of the character's

4
00:29.950 --> 00:35.800
rotation, we can push along any of these local vectors with the Translate function, by specifying movement

5
00:35.830 --> 00:43.300
in one (or more) of the x, y and z local axes. To move the character forward at a constant speed,

6
00:43.300 --> 00:49.720
we would modify only its z-coordinate. To move a character right at a constant speed, we would modify

7
00:49.780 --> 00:53.160
its x-coordinate and to move the character up,

8
00:53.170 --> 00:56.060
we would modify its y-coordinate.

9
00:56.100 --> 01:02.220
You can also move a character backwards, left and down by specifying a negative speed.

10
01:02.410 --> 01:07.450
If you modify the rotation of an axis while rotating it, the character will move in a different world

11
01:07.450 --> 01:08.400
direction, however,

12
01:08.500 --> 01:12.190
it will still be moving along its local specified axis.

13
01:12.220 --> 01:17.980
For example, if a character changes direction while being pushed along a z-axis, it will move around the

14
01:17.980 --> 01:18.910
world,

15
01:19.090 --> 01:25.060
but relative to itself, it will still be moving along its z-axis as it's the characters forward axis that

16
01:25.060 --> 01:29.020
has changed in the rotation and not its movement speed.

17
01:29.410 --> 01:31.950
So let's try this all out in a project.

18
01:31.960 --> 01:38.530
Download the project 'PushForward', which is the resource that comes with this lecture and when you open

19
01:38.530 --> 01:44.680
it up, open up the zombie scene (because everything's better with zombies) and open up and have a look

20
01:44.830 --> 01:51.490
at what you've got in there. If you press Play at this point, you'll notice that you've got an animated

21
01:51.490 --> 01:54.570
zombie that's not going anywhere...fast

22
01:54.700 --> 01:57.440
and a cube-looking thing.

23
01:57.580 --> 02:02.500
If you use the arrow keys, you'll find that you can rotate that cube and drive it around.

24
02:02.500 --> 02:11.650
This cube will be our goal object and by moving it we'll be allowed to see the rotation of that zombie and how

25
02:11.650 --> 02:13.720
we're going to push it forward.

26
02:13.720 --> 02:20.170
All right, so let's stop playing that. The cube itself, if you're interested, has a 'Drive' code on it and

27
02:20.170 --> 02:27.780
you can have a look at that script and what it does, it just drives the cube around. Again, it pushes it along

28
02:27.820 --> 02:34.130
just the same way as we're doing with our zombie character and also has a rotation in there as well,

29
02:34.300 --> 02:36.750
but that's not the focus of this lesson.

30
02:36.850 --> 02:44.590
We're looking at moving the zombie, changing its direction (so it's always looking at this cube) and pushing

31
02:44.590 --> 02:46.390
it along its z-axis.

32
02:46.390 --> 02:52.260
Now if you click on the model itself and have a look, you'll see it's z-axis is facing forward

33
02:52.410 --> 02:55.020
and this is what we're pushing it along.

34
02:55.270 --> 03:03.120
So create yourself a new C# file called 'MoveLocal'. In that script, you're going to add this.

35
03:03.130 --> 03:06.250
So it's similar to what we had in the previous lecture.

36
03:06.280 --> 03:12.720
We've got a 'goal' that we will pass in (that will be our cube) and we have a speed.

37
03:12.880 --> 03:19.690
Now this speed you'll need to adjust, based on the animation and whether it fits with it. Sometimes

38
03:19.690 --> 03:27.250
if your speed is going too fast or too slow for the particular animation, the character will slip and

39
03:27.250 --> 03:33.190
there are many ways you can fix this but we're not, again, going into all that depth with animation

40
03:33.220 --> 03:33.990
either.

41
03:34.120 --> 03:34.430
Right.

42
03:34.450 --> 03:42.100
So add that in and then in the Update, we have a transform.LookAt to change the direction that we're

43
03:42.100 --> 03:49.180
looking in for our zombie, which will be the 'goal' position and then we're going to push the zombie along

44
03:49.180 --> 03:56.260
it's z-axis using our speed and multiplying that by Time.deltaTime.

45
03:56.290 --> 03:58.750
Now I'm doing this in the Update

46
03:58.840 --> 04:00.080
and what did I tell you before?

47
04:00.100 --> 04:02.900
I always forget to put LateUpdate, so let's put that there.

48
04:03.130 --> 04:04.750
Now save that.

49
04:04.930 --> 04:10.550
Go back into Unity and you want to attach that 'MoveLocal' to Jill

50
04:10.570 --> 04:18.280
(Jill's the name of our zombie) at the very top level. Looking in the Inspector for Jill,

51
04:18.280 --> 04:24.670
she'll have a Transform, an Animator and then the script that you've added there. In the script, you'll

52
04:24.670 --> 04:33.580
see the 'goal' has been exposed, so get hold of your cube and drag-and-drop that in there as the top level

53
04:34.120 --> 04:34.560
goal.

54
04:34.600 --> 04:40.630
Ok, so now press Play and she'll start moving towards the cube.

55
04:40.810 --> 04:51.950
Now if we drive this away, because of the LookAt, she'll be constantly turning and looking at the cube.

56
04:52.070 --> 04:57.470
Everybody in this scene is currently floating above the ground but you could adjust that by getting

57
04:57.470 --> 05:05.030
hold of your character and driving it down into the ground and also grabbing your cube and driving it

58
05:05.930 --> 05:09.100
down like this.

59
05:09.100 --> 05:15.190
Ok, then have another play.

60
05:15.250 --> 05:20.570
Now you can see that the character is still floating a little bit above the ground

61
05:20.860 --> 05:25.600
and we have the same issue with the flickering that we have had in the previous lecture where the character

62
05:25.600 --> 05:30.760
will reach the goal point and then keep trying to reach the goal point, slightly overshooting and coming

63
05:30.760 --> 05:32.470
back and swapping directions,

64
05:32.470 --> 05:35.800
but if you move your cube away, it will stop.

65
05:35.920 --> 05:37.490
Now she's still floating in the air

66
05:37.530 --> 05:45.640
and this highlights another issue with this particular code, which is that she's going to change and look at

67
05:45.910 --> 05:46.180
that cube,

68
05:46.180 --> 05:50.750
wherever it is. The LookAt does not discriminate

69
05:50.830 --> 05:57.220
if the cube is up in the air. So let's move this cube like that and look how that LookAt is moving the

70
05:57.220 --> 06:03.880
z-axis, so that it will change and always be facing towards the cube.

71
06:03.880 --> 06:12.460
If we pause this and have a look at Jill, you'll see that her z-axis is being turned towards our

72
06:12.490 --> 06:19.300
cube position and this will cause a lot of issues when you start moving objects that really should stay

73
06:19.300 --> 06:26.380
on the ground (like a humanoid) to look at the goal position that is in 3D space and can be up in the

74
06:26.470 --> 06:27.200
air.

75
06:28.040 --> 06:36.230
To recap on that, what the LookAt is doing is rotating your model so the z-axis is pointing

76
06:36.230 --> 06:39.140
towards the position of the goal.

77
06:39.320 --> 06:42.410
So it doesn't matter where in 3D space

78
06:42.410 --> 06:50.060
that goal is positioned, it will orientate the 3D model, so that he's facing in that direction

79
06:50.240 --> 06:54.150
and this is a real problem for characters that need to stay grounded.

80
06:54.200 --> 07:01.550
Instead what we want to do is have our character rotate in the direction of the object but only in the

81
07:01.640 --> 07:10.310
x and the y plane, such that the z-axis then lines up with the position of the object on a vertical

82
07:10.310 --> 07:14.570
axis from it, at the exact same height of the character.

83
07:14.690 --> 07:21.420
Therefore we need the character to perform a LookAt at a point that is in the same y as the character,

84
07:21.530 --> 07:29.210
but the same x and z as the goal object. To achieve this in Unity is just a minor adjustment to our

85
07:29.210 --> 07:36.620
MoveLocal code. Instead of performing a LookAt at the goal (which is our cube that we're passing through),

86
07:36.710 --> 07:44.570
we calculate a new goal position to look at, which will be at the same x and z position of the cube, as you can

87
07:44.570 --> 07:45.450
see here,

88
07:45.620 --> 07:49.900
but it's y-position is the same as our character.

89
07:50.060 --> 07:57.590
Then we use this new position we've calculated (which I've called lookAtGoal and we use that inside

90
07:57.650 --> 08:07.430
of the LookAt instead. So once you've updated your code, if you save that, go back to Unity and run it, you

91
08:07.430 --> 08:15.110
can move your cube up in height (as you can see here, my cube's already floating) and it doesn't affect

92
08:15.260 --> 08:16.930
the zombie character whatsoever.

93
08:16.940 --> 08:24.070
So she's staying grounded and her z is therefore not being rotated in an upward direction.

94
08:24.080 --> 08:31.750
It is staying parallel to the ground.

95
08:31.760 --> 08:37.280
One last thing to complete our code is to have the zombie stop moving when it reaches the location,

96
08:37.280 --> 08:39.620
so that you don't get the 'jitteriness'.

97
08:39.620 --> 08:43.340
First of all, we'll need to add in an 'accuracy' value.

98
08:43.860 --> 08:46.870
So this is very similar to the code that we've done before.

99
08:47.780 --> 08:50.710
I'll set that to 1.0f.

100
08:50.870 --> 08:54.610
in this case and you can adjust that later.

101
08:54.770 --> 09:01.640
Then we want to test the accuracy before we do a Translate, so we'll still LookAt even if we're close

102
09:01.640 --> 09:10.760
enough, but we won't move any closer to it. So 'if' and in this case, we don't have a direction.magnitude

103
09:10.760 --> 09:17.840
to use as our distance to the goal because we've never calculated the direction, so this is a little

104
09:17.840 --> 09:20.270
bit different to the previous code.

105
09:20.270 --> 09:26.990
But what's really ice that comes with our vectors is that we can calculate the distance between 2 positions

106
09:27.290 --> 09:32.300
and we can do that with Vector3.Distance

107
09:32.450 --> 09:41.060
and we want to find the distance between our current location (which is transform.position) and

108
09:41.120 --> 09:42.150
the goal position.

109
09:42.170 --> 09:46.740
So goal.position

110
09:46.840 --> 09:57.130
So that's your 'if' statement and in there we want to test for the distance being greater than our accuracy

111
09:57.190 --> 10:01.990
which will allow us to keep moving forward,

112
10:02.110 --> 10:05.660
otherwise it won't run that Translate.

113
10:06.130 --> 10:13.060
So in working out this Vector3.Distance between these two positions, it is essentially

114
10:13.450 --> 10:20.350
the same thing as finding the direction (or the vector) between those two values by taking one away

115
10:20.350 --> 10:23.490
from the other and then using the .magnitude.

116
10:23.500 --> 10:29.470
It's the same calculation, it's just different ways of working it out. So you could go in here and work

117
10:29.470 --> 10:34.390
out the direction and get the magnitude of that vector, or you can use this distance function.

118
10:34.510 --> 10:34.720
Right.

119
10:34.720 --> 10:44.310
So save that and now we'll go back into Unity and run it...and drive around...let's drive a little bit closer.

120
10:45.950 --> 10:53.000
Now we can still see, in this case, that we get 'jittering'.

121
10:53.170 --> 11:01.300
The reason is that our current distance is calculating it between the distance of the actual cube

122
11:01.930 --> 11:04.500
and the distance of the zombie,

123
11:04.500 --> 11:10.060
but the zombie's not trying to get to the position of the cube, it's trying to get to the position that's

124
11:10.240 --> 11:12.770
under the cube at the same height.

125
11:13.030 --> 11:20.740
So it has very little hope (especially if this cube is right up in the air) for it being close

126
11:20.740 --> 11:25.180
to it because it's not being allowed to move in an upward direction.

127
11:25.360 --> 11:32.260
So we'll make that slight adjustment to our code, back in here. Instead of trying to get the distance between

128
11:32.800 --> 11:38.920
our character and the goal position, which might not be possible because that goal position is up

129
11:38.920 --> 11:44.190
in the air, then instead we just want to get from the LookAt.

130
11:44.220 --> 11:51.780
So we'll put that in there, save it and go back and run.

131
11:51.820 --> 11:57.350
Now when the zombie gets close enough to our cube, it will stop moving.

132
11:57.370 --> 12:01.960
Of course the animation is still running, but we're not working with that at the moment and it doesn't matter

133
12:01.960 --> 12:10.000
how high up in the air you move this and the actual distance between these two,

134
12:10.300 --> 12:19.660
it's only concerned with the distance between itself and this position here that's at the same height.

135
12:19.690 --> 12:27.570
So now I can drive this cube around and the zombie will still follow it but stay grounded...

136
12:29.470 --> 12:35.060
and when we get close enough, it will stop.

137
12:35.070 --> 12:39.150
That is how you move a character along its forward axis.