WEBVTT

0
00:11.580 --> 00:17.770
The way we have our current character set up is to use the LookAt to turn towards the target location

1
00:17.910 --> 00:22.980
but the problem with this is it makes your character snap instantly around.

2
00:22.980 --> 00:31.120
Instead we want to introduce a method so that our character will turn slowly towards the target.

3
00:31.260 --> 00:37.070
To do that, we're going to use Spherical Linear intERPolation or SLERP for short

4
00:37.320 --> 00:43.620
and what it does is to interpolate between the vector we're currently facing and the direction that

5
00:43.620 --> 00:45.790
we want to be facing in.

6
00:45.930 --> 00:53.010
It does this over a particular time period, so that you can turn by a little angle each time until you

7
00:53.010 --> 00:55.290
reach your goal direction.

8
00:55.290 --> 01:00.510
Let's start implementing this in Unity. So we're going to modify the previous project that we ended with

9
01:00.780 --> 01:03.300
inside of the MoveLocal script.

10
01:03.300 --> 01:12.380
The first thing we need is a float for the rotation speed, so let's call it rotSpeed and I'll set that to 0.5f.

11
01:12.460 --> 01:18.480
Chances are once you start moving your characters around, you won't want the speed and the rotational

12
01:18.480 --> 01:20.490
speed to be exactly the same.

13
01:20.610 --> 01:27.020
So having two different values works really well because you can adjust either one. Of course, in this case,

14
01:27.030 --> 01:28.520
I DO have them the same.

15
01:28.540 --> 01:32.400
In fact, I might just turn that down to 0.1.

16
01:32.400 --> 01:39.660
Now, we have a lookAtGoal, but we also need to calculate the direction that we want to be facing

17
01:39.660 --> 01:45.180
so we're going back to what we had originally and combining these two.

18
01:45.180 --> 01:48.720
So we want to work at our direction that we need to face.

19
01:48.720 --> 02:03.340
So it's a Vector3 direction and it's going to be equal to our lookAtGoal minus our current position.

20
02:03.390 --> 02:12.360
Now the only other change we need to do is to turn this LookAt here into a slerp that will use the direction

21
02:12.750 --> 02:14.070
as its bearing.

22
02:14.310 --> 02:18.340
So delete the LookAt line and replace it with this.

23
02:18.390 --> 02:26.850
The slerp is setting the rotation of your character, so therefore we're doing a this.transform.rotation =

24
02:26.910 --> 02:35.220
rather than a position or a Translate and this .rotation is then set using the slerp method

25
02:35.310 --> 02:44.400
that you can find in the Quaternion class. Quaternion is a complex data structure that's holding a rotation

26
02:45.030 --> 02:47.360
and it can do all sorts of fantastic things

27
02:47.370 --> 02:52.940
but in this case it's turning an angle from facing in one direction to another.

28
02:53.130 --> 02:57.920
So the slerp itself takes the current rotation of your character.

29
02:58.350 --> 03:07.350
It then performs a LookRotation on your direction, so direction is our vector we calculated which is pointing

30
03:07.350 --> 03:13.650
towards our goal, but we can't do the slerp on the direction vector.

31
03:13.650 --> 03:18.750
We want to do the slerp on the angle that we have to rotate through, to get from where we're currently

32
03:18.750 --> 03:20.940
looking to the particular direction.

33
03:20.940 --> 03:23.400
So this Quaternion.LookRotation will

34
03:23.400 --> 03:33.420
give us the rotation that needs to occur to be facing in 'direction' and thereby combining these two and

35
03:33.420 --> 03:36.130
comparing them little-by-little,

36
03:36.180 --> 03:39.970
then we slowly turn from one to the other.

37
03:39.990 --> 03:46.650
Now the rate at which we turn is the third value that you put inside of this method and we're basing

38
03:46.650 --> 03:54.210
it on the same type of speeds that we use for moving a character forward, where we've got deltaTime

39
03:54.210 --> 03:57.120
multiplied by our rotational time.

40
03:57.120 --> 03:57.300
Right.

41
03:57.300 --> 04:03.210
So let's save that and go into Unity and have a look at how this operates. Make sure your character

42
04:03.210 --> 04:11.420
is facing away from the goal, so that you can see the turning occurring. So press Play and the character

43
04:11.750 --> 04:17.020
will still be getting pushed forward along its forward vector, which is what we want

44
04:17.210 --> 04:22.700
and at the same time, that slerp is operating to turn your character around and you can see here in the

45
04:22.700 --> 04:26.130
Scene view that it's starting to arc around towards there.

46
04:26.140 --> 04:30.780
Now it's a little bit slow, this rotational speed, so let's just stop that.

47
04:30.950 --> 04:31.950
Go back into our

48
04:31.970 --> 04:33.710
MoveLocal and increase it.

49
04:33.710 --> 04:40.940
So, if we put that up to say 4 and save it and then run that again.

50
04:44.130 --> 04:53.280
We find that the character turns much more sharply and we can now drive our cube around and the character

51
04:53.280 --> 04:56.690
will turn and follow it.

52
04:56.730 --> 05:02.700
So that's how you move your character forward and how you perform a slerp to get your character facing

53
05:02.700 --> 05:04.140
in the right direction

54
05:04.350 --> 05:12.420
and just in these few videos, we have covered pretty much all the essential basics that you're going

55
05:12.420 --> 05:18.160
to need to get some very complex behavior happening in NPCs.