WEBVTT

0
00:14.490 --> 00:19.980
In this lecture, I want to address an issue that people have asked me a lot about in the Q&A section

1
00:20.040 --> 00:28.130
and that is synchronizing the agents speed with that of the navmesh agent. As you can see here,

2
00:28.140 --> 00:36.720
I've got a very simple waypoint system set up on a navmesh and Ethan demonstrating how his running

3
00:36.780 --> 00:43.650
animation is going way too fast for what he's actually doing, so it needs to be slowed down but somehow

4
00:43.650 --> 00:48.110
it needs to be synchronized with the speed of the agent.

5
00:48.120 --> 00:52.470
Now when I talk about the speed of the agent (let me just stop playing this),

6
00:52.530 --> 00:59.970
if we go back to our actual agent and have a look over in the Inspector, there's a 'Speed' that you can set.

7
01:00.000 --> 01:01.960
This speed is three point five.

8
01:01.970 --> 01:04.740
Now this is ALWAYS 3.5.

9
01:04.740 --> 01:12.080
It's the speed that you're allowing your agent to 'steer at'/'move between points at'.

10
01:12.180 --> 01:19.440
But it's not necessarily how fast he's going because he's going to slow down into corners and then accelerate

11
01:19.590 --> 01:20.310
out of them.

12
01:20.760 --> 01:27.780
So the value that you actually want to have driving your animations is the 'velocity' of the character

13
01:28.080 --> 01:32.760
and I'm going to show you how to assign that to the animations.

14
01:32.850 --> 01:33.150
Ok,

15
01:33.150 --> 01:38.120
so you can grab hold of this particular project, it's attached as a resource.

16
01:38.340 --> 01:46.470
Select 'Environment' and make it into

17
01:46.470 --> 01:52.740
a navmesh, so click on your Navigation tab or find it under

18
01:52.770 --> 01:58.830
Window > AI >Navigation and just make sure you click on [Bake] so that you do see this blue area when you try and

19
01:58.830 --> 01:59.070
run,

20
01:59.070 --> 02:01.000
otherwise you'll get an error.

21
02:01.020 --> 02:05.390
When you press Play, you'll see exactly what you're seeing here.

22
02:05.390 --> 02:13.080
Now I do have Ethan's animations turned up really fast on purpose, just to demonstrate how you can get

23
02:13.080 --> 02:17.390
animations that don't actually fit with the speed of this.

24
02:17.430 --> 02:24.300
Now the character himself is driven by the navmesh agent, so the speed and everything. The animation,

25
02:24.330 --> 02:30.690
if we have a look in the Inspector at the animation, I've got 'Apply Root Motion' turned off because we

26
02:30.690 --> 02:37.620
don't want the animation system trying to move the agent while the agent is actually being driven by

27
02:37.620 --> 02:38.640
its own code,

28
02:38.670 --> 02:40.880
so that does have to be off.

29
02:41.050 --> 02:41.300
Ok.

30
02:41.310 --> 02:43.500
So how do we sync up these two values?

31
02:43.500 --> 02:51.690
Well first of all we need to go into the Animator with Ethan selected and have a look in the parameters

32
02:51.690 --> 02:52.080
list.

33
02:52.080 --> 02:53.670
Currently it's empty.

34
02:53.670 --> 02:56.100
We're going to set a parameter in here,

35
02:56.100 --> 03:00.820
so we're going to add a Float and call it velocity or 'vel'.

36
03:00.870 --> 03:02.040
That will do.

37
03:02.040 --> 03:06.120
We can leave it set to 0, we're going to change this with the code.

38
03:06.120 --> 03:13.430
Now once you've got that, select the animation (there's only a HumanoidRun on there at the moment)

39
03:13.500 --> 03:20.330
then over in the Inspector you'll see that the speed currently for HumanoidRun is set to 3.

40
03:20.370 --> 03:25.920
We're going to multiply that speed by a parameter, so select that

41
03:25.980 --> 03:30.840
and then if you drop down the list next to it, you'll get 'vel'.

42
03:30.870 --> 03:36.180
So any parameters that you have in here, that are applicable for multiplying by speed, you can put in

43
03:36.180 --> 03:40.380
this dropdown list and control with your script.

44
03:40.410 --> 03:40.730
All right.

45
03:40.740 --> 03:46.680
We've got that, let's now open up the script that's on Ethan, which is this FollowPath

46
03:46.800 --> 03:49.590
and first we need the Animator.

47
03:49.710 --> 04:00.280
So let's get hold of that with 'Animator anim;' and then we will pick that up in our start with

48
04:00.280 --> 04:10.800
'anim = this.GetComponent' and then Animator and then finally, this is just a simple waypoint system going

49
04:10.800 --> 04:15.180
through each waypoint and updating them as we reach the destination.

50
04:15.180 --> 04:18.780
After we've done that, we're going to set the speed.

51
04:18.780 --> 04:28.980
So this will be 'anim.SetFloat("vel"

52
04:28.980 --> 04:32.700
and the value that we're going to set that to.

53
04:32.730 --> 04:38.940
Well that's the velocity and you can get that off the agent with 'agent.velocity'.

54
04:38.940 --> 04:41.540
Now that's a vector.

55
04:41.850 --> 04:43.430
You want the length of that vector.

56
04:43.740 --> 04:50.500
So '.magnitude' because that vector will change length based on how strong the velocity is.

57
04:50.550 --> 04:56.640
Okay so now this magnitude will be fed into your velocity. Save that once you've got it. Let's go back

58
04:56.640 --> 05:02.940
to Unity. Have the Animator window open so you can watch this 'vel' change with the speed of the agent.

59
05:02.970 --> 05:11.610
So press Play and then look over here. You can see how it changes and it actually goes down and then up again

60
05:11.610 --> 05:14.330
as he accelerates out of each of those.

61
05:14.340 --> 05:21.150
Now when currently multiplying 3.5 by...what's on here...3.

62
05:21.150 --> 05:24.470
So that is just way too fast as you can see there,

63
05:24.510 --> 05:25.860
so let's just stop that.

64
05:25.890 --> 05:32.320
Let's turn our speed multiplier down to say 0.3

65
05:32.580 --> 05:40.410
and then again press Play and now you'll see (let's go back into the Scene, so that we can see it better)

66
05:40.430 --> 05:47.060
that he is now much better on that surface.

67
05:47.060 --> 05:50.000
He's not quite touching the ground, but we can fix that.

68
05:50.000 --> 05:51.630
That's a navmesh thing.

69
05:51.710 --> 06:04.310
If we select Environment and go to Navigation, we can then drop down 'Advanced' and turn on the

70
06:04.310 --> 06:06.550
'Height Mesh' and then bake that in.

71
06:06.770 --> 06:16.320
That will allow him now when he runs to sit on the actual mesh a little bit better.

72
06:16.470 --> 06:19.470
That, of course, doesn't affect the animation whatsoever.

73
06:19.470 --> 06:29.670
But now you know how to pass through values from your script to your animator and then control that

74
06:29.670 --> 06:34.440
so that you have a more realistic procedural animation going on.