WEBVTT

0
00:11.380 --> 00:18.570
As we've seen, navmesh agent movement and animation don't work well together by default. We can't edit or overide

1
00:18.580 --> 00:22.290
the navmesh agent class to consider the animation of our character,

2
00:22.600 --> 00:24.670
so we need to come up with another method.

3
00:24.850 --> 00:29.930
Luckily Unity provides a helpful animation setup and some script for beginners.

4
00:30.250 --> 00:37.750
We're going to add some tools provided by Unity onto our zombie character to get her animation and her

5
00:37.750 --> 00:41.110
navmesh movement to talk to each other.

6
00:41.110 --> 00:47.410
Now you don't need to know a lot about the animation system to do this as the stuff provided by Unity

7
00:47.410 --> 00:49.980
has all the set up done for you.

8
00:50.050 --> 00:56.290
So with this lecture, you'll need to download the StrafeSetPackage and drag-and-drop it into

9
00:56.290 --> 00:57.760
your Asset folder.

10
01:03.610 --> 01:08.710
Once it's imported, you'll get an 'Animations' folder in your Assets and this is going to have all the necessary

11
01:08.710 --> 01:09.190
tools that

12
01:09.190 --> 01:16.420
we need to animate Stevie, so that she can follow us around properly and the animations will look nice

13
01:16.420 --> 01:17.610
and smooth.

14
01:17.980 --> 01:24.760
If you want to use your own character instead, you need an FBX file and a character that is rigged

15
01:24.790 --> 01:26.700
for the Humanoid system.

16
01:26.710 --> 01:33.390
Now if you're not an animator, you can get a whole bunch of free characters from Mixamo.com.

17
01:33.670 --> 01:37.990
So you go to Mixamo.com and you can log in with your Adobe account.

18
01:37.990 --> 01:44.600
You don't need to have a paid account, so this is a free service and you can select characters.

19
01:44.620 --> 01:48.790
You can also select animations (but you don't need any animations for this exercise)

20
01:48.970 --> 01:56.470
and you can pick whichever character that you like and then click on [DOWNLOAD] once they've come

21
01:56.470 --> 02:05.370
up and it will give you the option to download for Unity, which is just there and then you get your character

22
02:05.400 --> 02:07.810
and drag-and-drop it into your Assets folder.

23
02:07.920 --> 02:13.200
Then you can drag-and-drop it into your scene and then you can apply the exact same processes that we're

24
02:13.200 --> 02:14.120
doing here

25
02:14.130 --> 02:17.160
and also make them into a Nav Mesh Agent.

26
02:17.370 --> 02:24.540
When you first bring your FBX file from Mixamo into Unity, just drag-and-drop it from the desktop or

27
02:24.550 --> 02:32.260
wherever into your Assets folder and it's going to ask you to fix the normal, so select [Fix now].

28
02:32.270 --> 02:38.130
If you don't, it will lose all the connections to the textures, which is something you don't want.

29
02:38.550 --> 02:47.730
You're going to end up with a 'model component' and then a '.fbm folder' which has all of the textures

30
02:47.730 --> 02:55.110
in it and then a 'materials folder' as well. So this one here is the one that's got all of your animations

31
02:55.350 --> 03:02.730
and components and whatever attached to it. What you want to do is grab that and drag it into your scene

32
03:02.730 --> 03:05.820
which will now make your character part of your game.

33
03:05.880 --> 03:13.050
When you do that, something really odd with the materials will be going on and all of the Normals

34
03:13.620 --> 03:21.060
will have turned things inside-out and make it look a bit strange. To fix that, go into the Materials

35
03:21.120 --> 03:32.430
folder and select each of the materials (shift-select), go over to 'Shader' in the Inspector, 'Legacy Shaders' and select

36
03:32.430 --> 03:38.850
one of the legacy shaders. I usually pick 'Bumped Diffuse' which is the top one. That will fix your shading

37
03:38.850 --> 03:40.600
problem on the character.

38
03:41.510 --> 03:47.720
Now there's 2 other things you need to do to get your character ready in the same state as our zombie

39
03:47.810 --> 03:53.170
character that we've been using, in order for it to work with this set up.

40
03:53.180 --> 04:01.190
So select the character itself in the Hierarchy, you'll find the 'Animator' in there and this is where you drag

41
04:01.190 --> 04:03.230
that Locomotion controller in,

42
04:03.240 --> 04:04.960
over here later on.

43
04:05.080 --> 04:11.920
But what's important at this point is that you tick this 'Apply Root Motion' so that it'll be the animation

44
04:11.920 --> 04:17.230
that drives your character around as it's moving.

45
04:17.240 --> 04:25.460
The other thing you need to do is turn your character into a Humanoid. That happens in Assets, where your

46
04:25.460 --> 04:36.390
character is selected, then over in the Inspector you'll find 3 tabs: Model, Rig and Animations. We want

47
04:36.460 --> 04:37.340
the Rig tab

48
04:38.270 --> 04:43.190
and then go to Animation Type and set that to 'Humanoid'

49
04:43.190 --> 04:48.160
(by default, it will come in as 'Generic')

50
04:48.210 --> 04:57.330
and then click [Apply]. Now that new character is ready to do exactly what we did with our zombie

51
04:57.330 --> 05:04.000
model, so from here you can go and add the Nav Mesh Agent and you can add all of the other components.

52
05:04.440 --> 05:11.100
So that's a really quick overview of how you get a model from Mixamo into here

53
05:11.100 --> 05:16.750
and if you're not an animator, then Mixamo is a fantastic resource to have access to.

54
05:17.190 --> 05:24.360
So we've got our character. Here's Stevie set up exactly the same way as she was in the previous lecture.

55
05:24.390 --> 05:31.120
She's got an Animator on her which was controlling her walking animation that I already gave to you

56
05:31.290 --> 05:38.130
and then the ChasePlayer script and the Nav Mesh Agent. What we're going to do now to change her behavior is

57
05:38.130 --> 05:42.950
to modify the zombie here, in the Animator.

58
05:43.080 --> 05:49.550
What you're looking for (in this new Animations folder you would have downloaded) is the 'Locomotion'

59
05:49.830 --> 05:51.250
controller.

60
05:51.510 --> 05:53.440
There's 2 in mine.

61
05:53.550 --> 05:57.010
If you've got 2, there might be one with a '1' on the end.

62
05:57.150 --> 05:57.990
Just ignore that.

63
05:57.990 --> 06:00.000
In fact, I might just delete it to get rid of it.

64
06:00.030 --> 06:02.980
We only want this one that's LocomotionController.

65
06:03.030 --> 06:13.100
Now with that, let's select Stevie again, you want to drag-and-drop that into the spot on the

66
06:13.100 --> 06:15.870
Animator Controller.

67
06:18.140 --> 06:23.030
The final thing to get this to work is...if you have a look in your Assets folder, when you imported

68
06:23.180 --> 06:29.420
the StrafeSetPackage, you'll also get a Scripts folder which will have some scripts in.

69
06:29.420 --> 06:32.030
The one you want is the LocomotionScript.

70
06:32.030 --> 06:40.370
So drag-and-drop that onto Stevie and that will allow the animation

71
06:40.370 --> 06:47.060
system you've just added to talk to the navmesh, to work out where the character should be and make

72
06:47.060 --> 06:54.040
sure that the speed at which she's moving is coordinated with the speed of the animation.

73
06:54.050 --> 07:01.160
Now if you press Play and go for a walk (I'll walk backwards so you can see what's going on)

74
07:01.280 --> 07:06.780
you can see that Stevie is now grounded. She's not sliding.

75
07:06.840 --> 07:07.380
Sure, her

76
07:07.400 --> 07:12.830
animations have changed because we've had to use the custom ones that unity have given us for this particular

77
07:12.830 --> 07:13.900
package.

78
07:13.980 --> 07:16.800
Notice how her navmesh cylinder

79
07:16.820 --> 07:26.120
(the green one) is in quite a different position to her actual position because what's happening in that script

80
07:26.180 --> 07:33.500
is that it is negotiating the position of the model and the Nav Mesh Agent and coming up with

81
07:33.500 --> 07:36.910
an average in-between of where to put the models.

82
07:36.920 --> 07:43.390
So it's going to look realistic while it's moving around and chasing you at the same time.

83
07:43.490 --> 07:47.260
There's two other scripts that come with that StrafeSetPackage.

84
07:47.280 --> 07:54.470
There's a 'ClickToMove' and a 'LookAt' script. The ClickToMove script will get Stevie to move to

85
07:54.470 --> 07:59.540
the location that you've clicked on the navmesh, much like we've already done in the past.

86
07:59.570 --> 08:00.890
Let's just have a look at that though.

87
08:00.890 --> 08:07.280
So what we're going to have to do is turn off our first person controller and add another camera into

88
08:07.280 --> 08:10.750
the scene. So, right-click in the Hierarchy and select Camera.

89
08:10.970 --> 08:15.180
Make sure that's not childed to anything

90
08:15.200 --> 08:21.170
and then you probably want to set your camera up to look at the Scene view there. With the camera selected,

91
08:21.800 --> 08:25.460
GameObject > Align With View.

92
08:25.460 --> 08:31.430
Now when we press Play, we'll be able to click around here and get Stevie to move. Now select Stevie and we're

93
08:31.430 --> 08:36.440
going to turn off the ChasePlayer script because she's no longer going to be able to find us to chase and we're

94
08:36.440 --> 08:37.700
not using it.

95
08:37.700 --> 08:44.510
We're going to use this 'ClickToMove', so drag that over into the Inspector and attach that to your agent.

96
08:44.660 --> 08:52.610
The ClickToMove script uses the main camera to project the ray out into the navmesh area

97
08:52.610 --> 08:59.000
and in order to do that and find it, make sure you've got your camera set and tagged as MainCamera

98
08:59.000 --> 09:01.270
because we've turned off the FPS controller

99
09:01.290 --> 09:06.140
so the main camera will now be gone. We need to create a new one to make sure that it can be found using

100
09:06.380 --> 09:08.410
that MainCamera tag.

101
09:08.540 --> 09:19.440
OK now press Play and you'll be able to click around the environment and Stevie will move.

102
09:19.460 --> 09:24.320
Now you can see that she has a number of different types of animations that she can use.

103
09:24.320 --> 09:36.230
Can we go up there on the stairs? She can move sideways and she can also move forward and backwards

104
09:36.590 --> 09:41.880
and also on the spot and she has an idle animation for when she stops.

105
09:43.110 --> 09:45.330
So that's what you'll get in that set.

106
09:45.330 --> 09:49.750
Now the final thing that you do have as well is this LookAt script.

107
09:49.950 --> 09:58.110
It allows you to move your character's head to look where they're going, so it just makes

108
09:58.110 --> 10:00.120
it even more realistic.

109
10:00.150 --> 10:03.920
So grab hold of that look at and let's put it over here.

110
10:04.110 --> 10:06.100
Drag-and-drop it onto Stevie.

111
10:06.600 --> 10:10.500
The thing you need to set up in this LookAt is the head.

112
10:10.500 --> 10:14.630
It needs to know where that joint in your character is.

113
10:14.640 --> 10:21.030
So if we get Stevie and we drop down the Hierarchy, you're looking underneath 'Hips'.

114
10:21.060 --> 10:27.390
So this is the rigging that's going on inside the character (the bones of the character). Look down through

115
10:27.390 --> 10:37.290
Spine and then Neck and then Head. Get the Head and drag-and-drop it into the 'Head slot' on the LookAt script

116
10:37.770 --> 10:44.760
and this will now control the character's head using the built in inverse kinematics system that's in

117
10:44.760 --> 10:46.290
the animation.

118
10:46.290 --> 10:51.260
So press Play and it's very subtle,

119
10:51.260 --> 10:56.380
but you can see she does turn her head to the direction that she's going in.

120
11:00.480 --> 11:05.940
All right, so now that you've got 1 Stevie set up, you can actually duplicate her if you want and have

121
11:06.740 --> 11:07.520
2 or more.

122
11:10.100 --> 11:17.880
Press Play and they will act like Nav Mesh Agents...well because they are Nav Mesh Agents. So they'll keep

123
11:17.880 --> 11:25.560
their distance from each other, whilst trying to move and also use that animation set that was given

124
11:25.560 --> 11:27.330
to us by Unity.

125
11:27.330 --> 11:33.210
If you're interested in adding in your own animations and you have your own animations, you need a set

126
11:33.210 --> 11:37.520
of 9 animations and they're called a Strafe Set.

127
11:37.650 --> 11:44.880
So they're movement for a particular state of your agent (so say running) but there are 9 different

128
11:44.880 --> 11:49.990
phases of running. So there's forwards, backwards, running on the spot and then side to side and then all

129
11:49.990 --> 11:52.400
of the diagonal directions as well.

130
11:52.410 --> 11:56.070
That's the entire strafe set. To find where that is set up,

131
11:56.070 --> 12:02.820
if you go to the Animations folder and then double-click on our LocomotionController to open up your

132
12:02.820 --> 12:04.600
Animator window.

133
12:04.740 --> 12:09.740
All of this control that's going on is happening inside the 'Move' state.

134
12:09.740 --> 12:14.310
If you double-click on that and open it up, you'll find that it's a blend tree.

135
12:14.340 --> 12:19.540
It's quite complex and it's probably not a good idea to go mucking around in here unless you know about blend trees.

136
12:19.620 --> 12:25.140
But if you do happen to have all those animations and you want to change the animations, then I would

137
12:25.770 --> 12:31.130
suggest you don't change any of the 'settings', but the animations themselves are in here.

138
12:34.390 --> 12:40.780
So this is like running 0 degrees, which would be forward, then you've got running at 90 degrees

139
12:41.080 --> 12:44.770
and then you got diagonally forward, diagonally backwards...

140
12:44.920 --> 12:49.830
So this is what is called the Strafe Set and that's where that name comes from.

141
12:49.930 --> 12:56.210
If you have your own animations, you can drag-and-drop them into here to set them up for your character.

142
12:56.380 --> 13:04.210
But these ones have all been created specially to work with this system, which makes it look very realistic.

143
13:04.270 --> 13:10.890
So if you really do want to know more about this, you'll see a link come up on the screen to where this

144
13:11.070 --> 13:15.570
set can be downloaded from, in the Unity documentation.

145
13:15.760 --> 13:20.920
If you're an animator familiar with all this, then it will help you understand it on another level.

146
13:20.920 --> 13:31.570
So that is setting up complex animations of a character and using the navmesh pathfinding and navigation

147
13:31.570 --> 13:35.300
to make those things talk to each other and look coordinated.