navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

Animating Characters with Mecanim in Unity 3D

Animating Characters with Mecanim in Unity 3D

with Sue Blackman

 


Learn to bring your 3D game characters to life with the help of Mecanim, Unity's new character animation system. Start by importing characters and mapping their bone and muscle structure. Create animation controllers and add existing animations to help them move through and interact with their environment. Learn how to blend and mask animation clips to create sophisticated in-game animations on the fly. Author Sue Blackman guides you through the intricacies of Mecanim as you learn to control an assortment of game characters in a variety of situations.
Topics include:
  • Importing characters and setting scale
  • Setting up the controller
  • Creating transitions
  • Scripting the controls
  • Adding layers and layer masks
  • Handling nonhumanoid appendages
  • Configuring blend trees
  • Controlling states with speed
  • Adding a multistate jump
  • Targeting with IK (inverse kinematics)
  • Setting up generic rigs
  • Testing the character in the game

show more

author
Sue Blackman
subject
3D + Animation, Character Animation, Game Design
software
Unity 3D 4.0
level
Intermediate
duration
5h 28m
released
Jun 14, 2013

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00 (music playing)
00:04 Hi, I'm Sue Blackman.
00:06 Welcome to animating characters with Mechanim in Unity 3D.
00:10 We'll start by importing a few characters that were built in 3DSMax and then map
00:14 their basic bone and muscle structure so they can be animated using clips from
00:18 multiple sources. Next, we'll introduce animation
00:22 controllers that contain the states, transitions, and input parameters which
00:26 will allow you to activate and control different parts of your character's animation.
00:33 We'll see how to use scripting to drive the characters with player input and
00:37 also, orchestrate more complex movements like jumps.
00:41 We'll explore character interaction with objects within a game environment.
00:46 And finally, we'll take a look at controlling appendages on characters that
00:50 don't fit the standard humanoid form. Now let's get started with animating
00:55 characters with Mecanim in Unity 3D.
00:58
Collapse this transcript
What you should know before watching this course
00:00 Before watching this course, you should at least have a basic working knowledge
00:04 of Unity. If you've already worked through the
00:06 Unity essentials course, you should be good to go.
00:08 If not, you should be comfortable navigating the Scene View and be able to
00:13 manipulate the UI as needed. You should also understand the basics of
00:18 scripting in Unity, and be familiar with the way Unity scripts interact with each
00:22 other to control game objects. We'll be using Unity's implementation of
00:27 JavaScript for this course. You don't need to be able to create and
00:31 animate your own characters. But that's definitely an advantage.
00:34 In fact, I'm assuming that that's the reason why a lot of you are watching this series.
00:39 I've created the animations for this course in 3DS Max, using the CAT,
00:44 Character Animation Tools system. CAT is a great way to quickly prototype
00:49 animations, but you may also want to try using motion capture for your characters.
00:54 There are couple of free must-have Mecanim sample scenes on the Unity asset
00:59 store that contain several motion capture animations.
01:03 I encourage you to check them out if you haven't already.
01:05 So the first thing to do is get a clear idea of just what Mecanim does and
01:10 doesn't do. To start, Mecanim is an animation system
01:14 that was originally designed for characters.
01:16 With its introduction, Unity also announced its intention of eventually
01:21 incorporating their Legacy animation system into it.
01:25 This means that you can expect regular changes in it, as they both add new
01:28 features and sort out how they will handle the legacy functionality.
01:33 So let's look at its features. Mecanim has 3 major features.
01:37 The first is this bone and or muscle re-targeting system.
01:42 This allows you to use animations created on various different characters on your
01:46 own character. And, yes, for those of us that would
01:49 rather build characters than animate them, this is huge.
01:52 If character animation is your passion, this provides you with a means of
01:57 creating libraries of behaviors that you can reuse on a wide variety of
02:01 characters, an animator's dream come true.
02:05 The second important feature is its built-in state engine.
02:08 It takes care of transitioning the characters between states.
02:12 Does this mean no scripting is required? No, but it does help immensly with the
02:17 logic that runs the show. Mecanim provides a powerful way to blend
02:22 between animation clips with a variety of transition options.
02:26 Layers and mask let you combine animations on the fly, reducing the
02:31 overhead of multiple clips for related behaviors.
02:35 So you're probably wondering what it doesn't do.
02:39 Well, it doesn't magically rig your character for you.
02:42 You will still need to have your character fully ready to animate before
02:46 importing into Unity. Fortunately, there are several characters
02:50 available on the Unity asset store. And you can also find several excellent
02:54 courses on lynda.com on modeling and rigging.
02:58 So is Mecanim easy to use? It's not difficult, but it's a complex system.
03:03 And because it's complex, it's a very powerful means of animating and
03:08 controlling characters in Unity. We won't be getting too deep into it
03:12 during the course, but you should be able to gain a good working understanding of Mecanim.
03:16 So let's get on with the course.
03:18
Collapse this transcript
Using the exercise files
00:00 If you're a premium member of Lynda.com, you have access to the exercise files
00:04 used throughout this titles. The exercise files have been split into 2 downloads.
00:10 The first contains chapters 1 through 4. And the second 5 through 9.
00:16 Each chapter contains the materials needed for each movie.
00:19 Most of our movies will have the project used at the start of the movie.
00:23 The finished project for each movie is in the next movie's folder, as it's start project.
00:30 Several of the movies include the use of snippets where I've copied my code from
00:34 to paste into unities script editor. In some of the movies we'll be importing
00:39 various assets for use in the game project.
00:42 Those can be found in the movies exercise assets folder.
00:45 If you're not a premium subscriber to lynda.com, you don't have access to the
00:49 exercise files but you can follow along from scratch with your own assets.
00:54
Collapse this transcript
1. Importing a Character
Importing characters and setting scale
00:00 Let's start by creating a new project in Unity.
00:02 I'm going to go to File > New Project and navigate out to my Desktop and to My
00:10 Exercise Files folder, chapter 1, 101, and I'm going to create a new folder.
00:21 And name it Mecanim Course, and that is the project name.
00:28 Select folder, and now we're going to need a few packages, we need character
00:33 controller and we need a script. And because we're going to be needing
00:40 some textures, lets also bring in the terrain assets, and create.
00:48 Now the first thing I like to do is I like to change my setup.
00:52 From the default to the two by three. I also prefer the single column project view.
00:58 You can get to that by right-clicking and choosing One Column Layout but feel free
01:03 to use whatever you're comfortable with. Now, let's go ahead and save our scene,
01:09 File > Save Scene as, and we'll just name this one Character set-up.
01:16 And next we'll need some test subjects. So I'm going to go out to My Desktop.
01:21 In the Exercise Assets folder, you'll find the Characters folder.
01:28 Let's open it up and see what we've got. We've got several FBX files of
01:32 characters, and a few PSD files for their textures.
01:37 So this folder contains a few characters that were created in 3ds max, but were
01:42 exported as fpx files. Unity can read files directly from
01:48 several different modeling applications such as 3Ds Max and mia, blender and
01:52 several more, but internally it still converts them to the FBX format.
01:59 If you don't have the native application installed and licensed in your machine
02:02 you may not be able to read them in. Exporting to the FBX format ensures that
02:07 anyone will be able to read the files into unity...
02:12 So let's go head back to our exercise. Assets, copy the characters folder, then
02:17 we'll paste it into the Unity Project folder's Assets folder.
02:23 Now when we go back to unity, in the Project view, we can see our new folder.
02:32 And the contents. Notice that unity doesn't show us the
02:35 file format anymore. The blue box icon shows that it is an asset.
02:40 If we open it up, we can see that it contains all the parts that came along
02:44 with that FBX file. So the first thing I want to do once I've
02:50 brought in new assets, expecially characters, is I want to check their scale.
02:55 And to do that, I want to set up a nice little plane.
02:59 And then I want to set a cube in it, so I'm going to go to GameObject > Create
03:04 Other > Plane. This'll give me a ground plane.
03:07 And then I'm going to Create Other > Cube, and focus in on it.
03:15 Now, this guy, because Unity uses meters, if we look at his scale, he's 1 by one by one.
03:21 In Unity when we create a character, a normal sort of six foot high person would
03:26 be about two meters tall, so characters we want to be somewhere between This
03:31 height and double it. I'm going to go ahead and change the Y
03:38 scale so we can see a good generic height for the character.
03:45 And now we can bring our characters in, so I'm going to drag the assets in one at
03:52 a time... This guy's obviously too small.
03:58 Delores, on the other hand, is way too big.
04:00 And we'll bring Linny in. And he's not too far off.
04:08 And I'm going to bring one more, and that's the robot character.
04:13 So, there's three main types of character setups that you can bring into Mecanim.
04:17 Traditional-skinned characters Like Lenny here where the bones effect the mesh or skin.
04:23 Multi part hierarchical characters like robots, where the parts are rigid but all
04:28 linked, and pure bone systems without any meshes at all.
04:33 These contain just animations, and we have a little bit of everything in this folder...
04:39 The reason why we want to deal with scale first is because even though Unity is
04:43 very good about scaling complex hierarchies, once a character's been
04:47 configured, changing the scale will mean having to reconfigure the character.
04:53 So we always want to check the scale first.
04:56 I already know how big I want my characters to be.
04:59 Box man we're going to us as an example for the scaling problem later on.
05:04 So let's set him to .04. We select him in the project view, the
05:13 asset itself and in model, .04 and apply Delores needs to be point zero, zero,
05:24 zero four five and apply. Lenny needs to be 0.0075 and the robot
05:42 needs to be 0.03. And once again, we apply.
05:56 Lenny still looks a little bit tall, so I'm going to check my cube, and sure
06:02 enough, I forgot to bring it up. After I changed its scale.
06:09 So everything looks pretty good right now.
06:11 So these guys needed to be scaled because they've come in from a bunch of different
06:15 projects and different places and different times.
06:18 If your bringing in assets from you this asset store there scales are probably
06:21 going to be correct. Then again you may want to change the
06:25 scale of the character depending on what type of character he is.
06:29 So with the scales all sorted out, in the next video, we'll start configuring the characters.
06:35
Collapse this transcript
Preparing for character configuration
00:00 In our last video we got our character's scale sorted out, so we no longer need
00:04 the cube. I'm going to delete it from the scene,
00:07 and then I'll spin the scene so we can get a better look at our characters.
00:12 Now it's time to set them up. So once again, we head back to the
00:15 project view. So I'm going to start out with Linny, and
00:18 in the import settings we're going to go to the rig section.
00:22 This thing looks deceptively simple but it's not.
00:25 Under animation type if we click it, we have none, which is obviously for objects
00:30 that aren't going to animate. Legacy, which uses the old animation
00:36 system, and that's good for mechanical things.
00:39 Generic, the default for importing objects, and the last one is humanoid.
00:46 To get the most out of Mecanim, you'll want to use humanoid.
00:50 The problem is that humanoid only covers basic humanoid bone rigs.
00:55 Obviously, we're talking bipeds, so that's going to mean 2 arms, 2 legs, and
00:59 1 head just to start. And it actually works out to a 15 bone
01:04 minimum, and that includes things like feet, neck, hips.
01:08 Rib cage, that sort of thing. If your character doesn't meet the
01:12 requirements for humanoid, that's when you need to use generic.
01:16 This means you will not be able to take advantage of Mecanims's re-targeting functionality.
01:21 But you will have full use of the state engine that comes with it.
01:24 For skin character, we're going to go and use humanoid.
01:27 For the avatar definition, create from this model is good.
01:32 And then we need to click Apply, and once we've done that, we're going to configure.
01:38 The check mark means that Mecanim has figured out the configuration but we're
01:42 going to go in and check it anyway. And since clicking Configuration hijacks
01:47 the scene window, we're going to get a dialog that appears and wants to know if
01:50 we want to save the scene and I'm going to go ahead and say Save.
01:56 So you can see there's a ton of new things to investigate.
02:00 But this is a good stopping point. So let's click Done, so we can save the scene.
02:05
Collapse this transcript
Mapping bones to configure a character
00:00 I've just reopened Unity, I need to get back to configuring Lenny, so I need to
00:04 select him in the Project View and press the Configure button again.
00:08 And let's zoom out in the scene view so we can see what he looks like.
00:13 For humanoid characters, the configuration process sets up the mapping
00:17 that's needed for re-targeting the bone system.
00:20 With different animations and characters. So the first thing we'll see in the
00:24 inspector is the Mapping Setup window. This character came in with all bones
00:28 recognized and correctly assigned. So the body parts in the image are fully green.
00:34 Looking below the picture in the assignment area, the first thing you'll
00:38 want to make note of is the character's body parts do not necessarily match the
00:41 Mecanim names. Mechanim calls it hips, we've called it root.
00:48 Spine, spin o two. Chest, spine o three.
00:54 So you can see the names don't necessarily match.
00:56 Mechanim looks for several key words when setting up the avatar, as well as their
01:00 positions in the hierarchy. It usually does a pretty good job with
01:04 the auto mapping. You'll also notice that many of the bones
01:08 are marked as optional. That's this little dotted line around the icon.
01:12 In the body section there are 14 bones that are mandatory.
01:16 The minimum number of bones required by a humanoid is 15, so we're one short.
01:22 If we look in the head section, we'll see the final bone, head.
01:26 When making the assignments, Mecanim starts with the root object, which
01:29 usually assumes to be the hips or pelvis, and then attempts to work out all the
01:33 rest of the assignments. In the Heirarchy view, just in case you
01:37 need to manually reassign some of them, the character's heirarchy has been fully expanded.
01:44 Let's select the left hand view, and click on a digit in the assignment list.
01:49 It selected in the Configure window, the hierarchy view, and the scene view.
01:53 If we select the leg bone in the scene view, the Configure window switches back
02:00 to the body. And the leg bone is selected in all four places.
02:06 Look for the pale blue circles in the list in the body icon.
02:10 Now let's take a closer look at the scene view.
02:13 Most of the bones are bright green, but a few are gray.
02:16 The gray bones are not included in the re-targeting solution.
02:20 Whatever bones are auto-mapped, you may wish to tweak the assignments.
02:26 But I've usually found that Mechanim does a pretty good job on its own.
02:29 Let's try remapping one of the bones just to see how it works.
02:33 In the spine section let's try spine 01 instead of spine 02.
02:38 I'll click it in the Scene view, and you can see it gets selected here.
02:44 And we can drag it straight into the slot, and that's how easy it is to change.
02:49 I'm going to go ahead and put spine two back into the original slot.
02:54 So that's a first look at mapping the bones and the configuration process, and
02:58 once again, I'm going to click Done so I can go ahead and save my scene, and Apply.
03:05
Collapse this transcript
Configuring muscles
00:00 The Lenny character that we setup in the previous video should do well with
00:03 animations from any humanoid character. But what if we need to use, say, a walk
00:08 cycle from a skinny character, on a chubby character.
00:13 Well it turns out, we can set limits on our character's range of movement, so the
00:17 animations will behave well, even when the character is virtually different.
00:22 So let's configure the Dolores character next.
00:25 In the project view, I'll select Dolores and rig.
00:29 we're going to set her to humanoid before we click apply.
00:34 Let's just remind ourselves what Dolores looks like.
00:37 There she is in all her beauty. We'll click Apply, for the humanoid.
00:42 And notice she has an X next to configure.
00:45 This means that Mecanim wasn't able to configure her automatically.
00:49 So we'll definitely need to go in, save the scene at the dialog, and see what's
00:53 up with it. Considering that none of the bones have
00:56 meaningful names its surprising that mechanim has done as well as it has.
01:00 Let's start with the head it looks like it should be bone 8 if we click it in the
01:05 view port. So let's go over to the head section.
01:10 Drag bone eight into head. We get an arrow that says head transform
01:15 bone 08 is also assigned to the spine. Don't worry about it we're going to
01:20 reassign that too. So lets assign that one next.
01:24 We'll take bone seven And assigned it as the spine and we get some green.
01:29 It also looks like the arm bones need to be moved up the hierarchy.
01:34 So, I'll drag them into the correct slots next.
01:37 I'll start with the shoulder. I don't have much real estate here with
01:41 this resolution but we'll see if we can find it.
01:44 So we want bone 11 for shoulder, bone 12 for upper arm, bone 13 For lower arm, and
01:53 we're probably fine with any of the bones below that.
02:02 So that's 14 for the hand. And that one goes green.
02:09 Find our right arm, so bone 23 gor shoulder.
02:16 Twenty two, his upper arm, twenty, his lower arm, and twenty one, his hand.
02:24 We're all green in the mapping window, but in the scene view we have a problem.
02:34 It shows that our character's not in t pose.
02:39 So at the bottom, from the pose drop down, We're going to select enforce t
02:43 pose, and this time it works. So we can click apply and move on to the
02:49 muscles, and we'll need to center her in the view a little more so we can see
02:54 what's happenning, and we've got some interesting sections here.
03:01 The muscle group preview is a stress test on steroids.
03:07 Let's see what we can do here. So, we can put our character into the
03:11 extremes using the different muscle groups.
03:16 And when we're finished, we can hit Reset All.
03:21 The important one here, is per muscle settings.
03:24 This is where we're going to be able to keep her arms from going through her body
03:28 when she's using the skinny character's walk cycle.
03:32 So, we're going to open. The left arm, and we're going to want to
03:37 change arm down, arm up. Here again is our preview.
03:43 This shows us the full possibility of the limits.
03:46 You can see that up is okay, but down, she's way too far into her body.
03:51 So I'm going to leave it over on the far let, and open up the arm up down Section,
03:55 I'm going to need a little more space here to see my numbers, but this shows us
04:00 the range, and so I need to select the right side and pull it out to about here,
04:04 and just to double check I'll switch over to the other extreme and that's one is
04:09 okay, so I think we'll leave that one as is.
04:18 So let's go down and do her right arm for the same setting, arm down up.
04:23 Once again, I want to see what my extremes are.
04:28 So I'm going to leave it to the far left and adjust the left side of the range slider.
04:36 So that's going to be as close to her body as her arms are going to be able to get.
04:40 The last section is additional settings, the main one we're going to want to use
04:44 here is feet spacing but you won't be able to see what that does until we
04:47 actually have her in scene. So we'll come back to that later.
04:52 So let's go ahead and click Apply, and now this character is fully set up, so we
04:57 can click Done and get back to our regular scene.
05:03
Collapse this transcript
Addressing import issues
00:00 The first character, Linny, was pretty easy to set up.
00:03 But things don't always go smoothly, depending on the source of your
00:06 character, as you saw with the Delores character.
00:09 In this video, we'll check out two of the other characters and discover a few
00:13 common problems you may need to deal with.
00:16 This time, let's select the robot, in the Project view.
00:20 Set his animation type to humanoid, click Apply.
00:24 He's got a check mark which looks good. But we'll go into Configure, an save the scene.
00:30 An he's all green. He looks pretty good.
00:35 But this guy didn't come in, in T pose. So let's try something else here.
00:40 Under mapping, we have the option to clear the map and auto map again.
00:45 So I'm going to show you what happpens if we do that.
00:48 I'll click Clear. Nothing's mapped, so everything goes red.
00:53 And then I go back in, and say automap, he's green again in the mapping view, but
00:57 in the seeing view, it shows that he's not in T pose.
01:02 Let's see if we can fix that. Under pose, just like we did with the
01:07 previous character, we're going to try and force T pose.
01:11 This time it worked. But sometimes you'll find that you'll
01:14 need to do a reset before you do the Enforce T-Pose.
01:18 We're good to go though, so we're going to go ahead and click Apply, and
01:24 Done, and move on to the next character. This time we're going to do BoxMan.
01:32 So Rig, and for animation type, Humanoid and Apply.
01:38 Now as soon as we hit Apply, we're going to notice something different with
01:40 this guy. He goes into a crawl mode, and if I zoom
01:44 out in the scene view, you'll notice he's not only in a crawl mode but he moves way
01:49 off his original position. That's because his animation ended on the crawl.
01:57 That's an issue we're going to look at later, but it's worth noting now.
02:01 So now we'll go ahead and click configure.
02:04 Save the Scene, and he comes in all green and happy.
02:08 But remember that warning about changing the scale factor after setup.
02:13 Lets take a look at it with this guy. Let's go ahead and say done.
02:17 Then back in model section, let's set a scale factor to 0.055 to put a more human
02:25 six foot four height. And apply.
02:33 So he's a little bigger in the view part and he's moved forward, but we have
02:36 another problem. He needs to be reconfigured.
02:40 If we switch back over to the rig section, and click on configure, save the scene.
02:49 And zoom in on him. We'll see that his body parts are all
02:55 folded up on each other. That's because the mesh objects rescale
03:00 to be larger but the bone system has not, so we need to reconfigure him to get him
03:04 pack to a good position again... So the first thing we might think about
03:10 doing is going down to mapping and saying Clear, Mapping, Auto Map, so all of a
03:16 sudden a character that came in perfectly fine before is starting to have trouble
03:21 with his bone mappings. We aren't even worried about his
03:28 character t pose... We go down to the pose, reset, enforce T
03:33 pose, it gets even worse. So things are definitely going from bad
03:39 to worse. Sometimes if you're having trouble with a
03:44 character that was okay before, it's better to click Done Revert, and in this
03:50 case the only way to fix him quickly is to go to Generic and hit Apply.
03:58 And then go back to Humanoid. Apply.
04:04 And now. We zoomed out.
04:10 We can see that he's got his new size, but let's check his configuration.
04:16 And sure enough, his bones and his mesh Match.
04:26 So let's click Done and look at our last character.
04:29 This guy was imported only with his bone structure and animations, so he doesn't
04:36 even have a mesh. So let's go over to Rig Set it to
04:41 humanoid, apply and let's check configure, save the scene and as you'd
04:46 expect everything came in well. You didn't have an mesh to get out of whack.
04:54 But the interesting thing here is the scene where we can see His bone set up.
05:01 Even though we think of the root of the character as being at the hips or the
05:05 pelvis we also need to transform root down at the feet.
05:11 It makes setting up clips and mecanim much easier.
05:14 I set up several of my animations using cat tools in 3DS max.
05:19 Cat, c a t, stands for character animation tools.
05:23 Those characters come in with the base platform that already exists for the
05:27 purpose of a transform root. For the robot I created a helper object
05:31 at the feet... And link the pelvis to it, before exporting.
05:36 So that's just one more thing to keep in mind, when you're creating characters
05:39 from Mecanim. Lets go ahead and click Done.
05:42 So now you've seen a variety of problems and solutions that you may have to deal
05:46 with when importing characters to use with Mecanim.
05:51
Collapse this transcript
Defining animation clips
00:00 With the characters configured, the next thing we'll need is a few animations.
00:04 We'll mostly be using animations from the characters I've provided for the course,
00:08 but you can also import other characters just for their animations.
00:13 As mentioned in the What You Need to Know video, Unity has a couple of free
00:16 Mechanim sample projects that contain a bunch of motion capture animations.
00:21 You can get them from the asset store. But let's go ahead and start by selecting
00:26 our BoxMan in the Project view and then selecting the animation section.
00:30 In the final chapter of this course you will be running a character through a
00:34 little obstacle course, so BoxMan has a few odd behaviors that may come in handy.
00:40 Take 001 should already be selected and the options showing for it underneath...
00:46 Let's click the Play button for the Preview window and check them out.
00:50 He's got a warming up idle, an on your mark pose.
00:59 A regular idol awaiting through something deep, and a fast crawl for a tunnel or something.
01:07 Depending on what program the character was animated in, and how he was exported.
01:12 The clips may or may not be set up for you.
01:15 In this case the take001 clip contains all the behaviors on a single timeline.
01:19 So we'll need to define each of the behaviors as separate clips.
01:23 The first thing I want to do is name the clips.
01:26 Before you do though, consider that you'll be able to use a variety of
01:29 animation clips from various sources. The problem with that is that everyone
01:34 tends to name them the same. Idle, walk, run, etc, so you could end up
01:38 with several clips of the same name and no quick way to choose the one you want.
01:45 For what reason I generally preface the clip name with an identifier.
01:50 With this guy I'm going to use BM for Box Man..
01:54 _. And the first clip I want is Warm Up.
01:59 So we do that next to the field that has the Clip icon.
02:02 And Enter. Above the Timeline you can see the length
02:06 of the clip in seconds. This one is 17.333.
02:12 And the frame rate, that the animation was recorded in.
02:15 In this case, 30 frames per second. Unity is very good at working at whatever
02:19 rate was used to create the animation. So, if you created something in Maya, at
02:24 24 frames per second, or in some other package at a different frame rate, Unity
02:28 would be able to handle it gracefully. In this clip our start frame was zero, so
02:34 we're good to go with the start. The end frame will be 240.
02:39 But before you type it in, let's take a look at the red dots next to all of the
02:43 loop animation labels. And I'm going to shrink my preview window
02:47 so we get a better shot at them. These tell you when the clip will
02:51 seamlessly loop in the animation. BoxMan, if you remember, ended his
02:57 animation in a crawl, so they're all red. No match.
03:02 Since this animation needs to loop, let's check Loop Pose and click Play.
03:08 Also, I'm going to right click on top of the Preview window's title bar so I can
03:12 float the window. Now if we click Play the animations are
03:20 distorted as Mecanim tries to resolve the start and end poses.
03:25 Let's go ahead and type in 240 for the end frame, and they all turn green...
03:35 Now if we click Play in the preview window, the animation should loop seamlessly.
03:39 It's pretty slow, so let's see what it would look like if we played it back faster.
03:45 In this section, we can speed it up, two times the speed it was recorded at.
03:50 Now let's stop and put the playback slider back to 1.
03:55 Let's load the next clip. Under clips, we need to click the plus to
04:01 get a new one. We'll name this one BM on your mark.
04:06 And enter, its start is 251 and its end is 280 but don't type it in yet.
04:14 Let's drag the starting n markers to get to the right frames.
04:19 So if I select the marker, and I drag it I'm going to get it back to 251.
04:26 As soon as we move the marker, we see a little curve of the animation in a couple
04:30 of the transform sections. So you can see where the clip of the
04:34 range becomes apparent. We can zoom and pan the timeline with the
04:38 usual Unity methods. Middle mouse roller will zoom, middle
04:45 mouse button held down will pan, and we can also grab the slider underneath to
04:51 adjust the timeline that way. I'll go ahead and type that end number
05:01 since I know what it is, 280. And we're still green so, everything's good.
05:07 On this pose we have to make an executive decision.
05:10 We have to decide if it should loop or not.
05:12 I'm going to say that once the starter starts the race, the player won't be able
05:16 to move the character until the next instructions are given.
05:20 So, I'm not going to loop this pose. Let's set up the next pose, get set.
05:26 Once again I'll click my Plus button. This one is BM get set, and I'll hit
05:30 Enter and there's something else we should look at here.
05:36 As we scrub through the timeline, and we can also do it in the preview window by
05:41 dragging the marker... You'll notice that the transition from
05:48 the warm up is immediate. One of the things that Mecanim takes care
05:55 of for us is the transition between the different animations.
06:00 So when you're creating your animations you no longer have to make transition period.
06:05 So this one goes directly in. The get set pose is just slightly
06:10 different, his head goes up. So let's put the numbers for that
06:16 according to my notes 283 to 298. And everything goes green.
06:23 This one I've decided the player will have to manually start the forward motion.
06:28 So I'm going to let this one loop until the player tells it otherwise.
06:34 Now let's click Play. And you can see there's a little bit of a problem.
06:39 I may have written down those numbers when I was animating him but something's
06:43 clearly off. So this is a time when you can go in and
06:47 you can adjust your poses. I'm going to stop play back there.
06:52 And, it looks like I need to tighten up my range a little bit.
06:57 So, I'm going to let the start, start one more frame on, at 284 and I'm going to
07:02 end a few frames sooner, 295. So if you bring in an animation that
07:09 isn't quite right, you've got a lot of flexibility to be able to tweak it and
07:12 make it work better for you. There's one more thing of interest with
07:18 this particular clip. The next clip is just a generic idol Be
07:24 in idle so once again I'll add a new clip type in BM idle hit enter and its numbers
07:33 are 301 to 380. Of interest here is the tracking used in
07:41 the preview window. If you spin the view so that box man is
07:44 facing you, you can see that the camera is following the hips or pelvis area,
07:48 making it look like he's on a balance board or something.
07:54 We'll deal with that later. So these are pretty typical set ups.
07:58 For animations where the character is not moving away from the starting position.
08:02 So let's click Apply, and make sure that our clips get saved.
08:07
Collapse this transcript
Working with moving characters
00:00 So far in the clips we've set up, the character hasn't gone anywhere.
00:04 But in the last two clips, he's actually going to be moving forward through the scene.
00:09 So lets select Box Man again. And in the animations panel, we're going
00:14 to set up a new clip. Click plus again.
00:19 This one is going to be named BM_BogWalk because our character is walking through
00:26 something deep. This one goes from 390 to 450, and it loops.
00:38 This is the first clip where the character actually moves forward through
00:41 the scene. If I click play, you see he's actually
00:45 moving forward there. So we don't care about his XZ not getting
00:50 a loop match. Underneath that is the area that shows
00:56 velocity, and you can see that his Z velocity is about 1.15.
01:04 The Z value is generally considered forward in Unity.
01:07 This tells us that he was not animated in place, but more importantly it will allow
01:11 Mechanim to move the character according to the animation clip's root motion.
01:17 If you were to select the other clips you would see that they're pretty much zero
01:21 for their Z velocities. The final clip BM_FastCrawl is a
01:26 specialty case. Let's go ahead and set it up.
01:30 Add Clip, Enter. And let's set the start marker to about halfway.
01:37 Let's click play and watch the ground plane.
01:45 Besides looking pretty silly, a fast crawl has an up axis, or Y, height
01:49 change, where he has dropped to his hands and knees.
01:54 In several of the clips, especially the crawl, the character's pelvis stays up at
01:58 its original position. So there, the ground appears farther
02:03 below the character than it should be. If you've got Pro, the shadows make this
02:09 very obvious. This is when you want to use Root
02:12 Transform Position Y Bake into Pose. So we'll go ahead and we'll click Loop Pose.
02:19 And Root Transform Position Y Bake into Pose.
02:23 It turns out that unless you have a clip such as a jump or a fall, where the Y
02:27 transform will be controlled by physics or some other outside entity, Bake into
02:31 Pose for Y Root Transform Position should always be checked.
02:37 So let's go back and turn on the rest of the clips bake into pose for the Y root
02:41 transform position. And back in the fast crawl, let's set its
02:47 start frame to 460. And its end frame to 520.
02:54 Which it already is. And now we can go ahead and click Apply
03:00 to save Box Man's new animation clips. I'll go ahead and dock the preview window
03:04 by clicking on the X. And now when we open Box Man in the
03:09 project view, we can see our newly-added clips.
03:16 Just out of curiosity, let's check the bones only version of Box Man.
03:21 He's already configured, so let's see him in the animation section.
03:24 Just bones. He's got his take one.
03:28 But if I right-click to undock the preview window, you can see that because
03:33 there was no mesh associated with it, Unity previews it using Dude, the default
03:38 avatar character. Let's click Play and see how the
03:43 animations map onto a taller character. And you can definitely see him crawling
03:50 above the ground until we say root transform position bake into pose.
03:59 And now he stays down where he should be. So our animation clips are all set up
04:03 now, and we're ready to move on.
04:05
Collapse this transcript
2. Animator Controller
Understanding the Animator Controller component
00:00 At this point, we've configured the avatar and set up animation clips for one
00:03 of the characters. The next step is to create the animator controller.
00:08 This is how we access Mechanims powerful state engine.
00:12 Once set up, it can be used to control various different characters.
00:17 To create one, we need to open the right-click menu, and from Create, all
00:21 the way down near the bottom, select Animator Controller.
00:26 And a new one is created for us. Let's name this one test controller.
00:34 Has a nice icon, but there's nothing showing in the Inspector.
00:38 Turns out the animator component is a fairly complex critter.
00:41 It has an entire window of its own with individual elements that use the Inspector.
00:46 To open it for the first time, we could go the Window menu and select it, or we
00:51 could just double-click on it. It opens in a tab next to the scene view
00:57 in the two by three layout. Now I'm going to give myself a little
01:01 more space. By default, it's showing the base layer
01:05 as you can see in the title bar in the upper left.
01:10 The name of the controller is shown at the bottom right.
01:12 Testcontroller.controller. In the top left is the layers section.
01:19 Layers will allow you to use masks to override parts of the current animation.
01:25 An example is a character that is in walk behavior where the layer using an arm
01:29 mask will allow just the arm to play a different behavior, such as waving his
01:32 arm or shooting his weapon, all while still walking.
01:37 You can create a new layer by clicking the plus in the layers bar.
01:42 You'll need to navigate the layer using the scroll bar, because you can't size
01:45 this guy. It will get bigger if you add more layers
01:49 to it, but it has a limit. To delete the layer, click on the minus sign.
01:53 For those of us who are used to closing roll downs with the minus sign, Unity
01:57 added a query box to make sure that's what you wanted to do.
02:01 Because, as the message says, you can't undo this action.
02:05 In the lower left is the parameter section.
02:08 You click on the plus sign and are immediately asked to choose what type of
02:12 variable you want. We have a choice of vector, that's your
02:17 basic multi-part variable, xyz, rgb, rgba, that kind of thing.
02:23 And you also have floats, ints, integers, or bools, Booleans.
02:29 After choosing, you can set their initial values and name them.
02:33 To get rid of them, click the minus sign. This time there's no warning, because
02:38 they aren't being used yet. And finally we have the state window.
02:43 You can navigate the window with the regular pan controls.
02:47 Middle mouse down, or alt and left mouse. And rolling the middle mouse roller pans
02:52 the view up and down. The only thing in it so far is the any state.
02:56 It allows your character to transition into a particular state, such as a dead
03:00 state from any other state without having to hook it up to each state individually.
03:06 So this is pretty much the basic layout of the animator controller window.
03:11
Collapse this transcript
Understanding the basics of state machines
00:00 If you're new to state engines, they can be a little confusing.
00:03 In this short video, we'll go over the basics so you can become familiar with
00:07 the concepts and terms before jumping into Mecanim's state engine.
00:12 A state engine is a way to organize, track, and orchestrate everything that
00:16 happens when an event occurs. To manage a state you need to know four things.
00:22 What state the object is currently in, what event can cause it to move to a
00:26 different state, what state the transition takes us into, and what
00:30 actions are involved to make the transition into the new state?
00:37 Typically, a simple state diagram might look like this, with the states in boxes
00:41 and the arrows representing the transitions.
00:45 Including the transitions' actions gives a bit more information about the
00:50 transitions by showing the animations that they contain.
00:54 But, with Mecanim, where you are controlling characters, you're usually
00:58 transitioning from one looping animation to another, so the animation clip becomes
01:03 the state. In this case, the transition becomes the
01:08 blend between the two animation clips. To add a bit more information, you can
01:14 add the event that triggers the transition.
01:18 If the animation is not looping, the event's animation time may be the end of
01:22 the animation. So the most important concept is that
01:26 transitions are associated with the state they put the object into, not the state
01:31 it just left. In effect, under the hood, pun intended,
01:36 a state engine is more or less a bunch of conditional statements, that manage the
01:40 transitions into the states for you. You'll still need to do scripting to
01:46 control the events, but Mecanim will be in charge of executing the transitions,
01:50 and updating the states for you.
01:53
Collapse this transcript
Setting up the controller
00:00 Now it's time to see how the state machine theory plays out in Mecanim.
00:04 Since you've already opened the animator controller view once, it will stay out in
00:07 Tab, so you can get to it from there, or you can always double click the
00:10 controller in the project view again. When returning to it, you should always
00:15 check to see which controller is showing. Since it may not reflect what you've got
00:20 selected in the rest of the editor, we only have one at this point, so we're
00:23 good to go. Lets see about creating a new state.
00:28 There's two ways to do that. You could right click in the View.
00:33 Go to Create State and Create Empty or you can just drag the clip into the view
00:37 and have it automatically created. So I'm going to locate one of my clips,
00:44 so I'll locate the bm idle clip under the box man asset and drag it into the view.
00:52 And whoola, there we have it, our first state.
00:56 And it's all nice and orange indicating that it's the default or starting state.
01:01 Additionally it's already named after the clip it represents.
01:06 If we click on it you can see the name up here, and I'm going to shorten the name
01:09 to just Idle and Enter. Now before we go any further there's a
01:14 couple of things I need to do. I realize that my box man's...
01:22 Idle clip didn't get checked with the loop pose, so I'm going to do that now.
01:28 And apply, and the next thing we need to do is tidy up our scene view.
01:37 So I'm going to click on the scene tab, zoom out a bit And I want to go ahead and
01:43 get my characters lined up better. BoxMan is sitting out there in his crawl
01:52 pose so we'll just not worry about him, and the next thing I want to do is select
01:57 my plain, and I want to scale it to ten by ten...
02:09 And to make it easier to look at, I'm going to drag, a texture onto it.
02:15 (UNKNOWN) it. And since I scaled my plane, I need to
02:18 scale the material as well. So I'll click on my material.
02:26 An I'll change its tiling to ten by ten as well.
02:33 We also might need a light. So I'll go to GameObject>Create Other>
02:41 Directional Light. Switch over to Global.
02:49 And we ought to rotate it so our characters will be lit.
02:54 If you've got Pro, you're probably going to want to turn on shadows as well.
03:02 And I prefer to turn my gizmos to regular and I might even turn that one off.
03:12 Here it is. And let's go ahead and save the scene,
03:19 since we've now got it set up. This base scene is going to be handy
03:25 throughout the series, so let's go ahead and create a folder called Scenes...
03:32 Drag our character setup screen into it, and I want to go ahead and duplicate it, Ctrl+D.
03:39 And let's name the duplicate base scene. We'll be using that several times.
03:48 Double click the character setup scene to get back to it.
03:52 And we'll good to keep on going here. When we set up the humanoid animation
03:55 type, and animator component was added to each of the characters.
04:00 If we look at them we'll see there's our animator component so now let's go ahead
04:05 and drag the fledgling test controller onto each of the characters' controller
04:09 field, and I'm going to go ahead and drag it out of the characters folder so that
04:13 we can find it easier, so we'll drag it onto.
04:21 The controller field on Box Man, and it turns out we don't even need to go to
04:24 that much trouble, we can just drag it directly onto the characters and it will
04:28 find its way to the right place. So the right thing we ought to do before
04:35 we click Play is get the camera sorted out...
04:42 I'm going to rotate my view a bit and then select my default camera and use
04:48 align with view so I'll be able to see it in the game window.
04:55 And finally let's click Play and see what happens, and I'll go ahead and stop play mode.
05:03 So they each, including Lenny who didn't even have an animation, go immediately
05:08 into the idle behavior. But we can also see a couple of new issues.
05:14 The first is the robot's orientation. If we went into Rig and Configure we'd be
05:19 able to see that his root shows as z is up.
05:23 He was created in 3D max but he inherited the scene orientation since his root or
05:27 pelvis was not parented to a help object. Turns out that was a different robot.
05:33 Let's replace him with the version that was parented to a helper object that I
05:37 created in Max's front view port, so we're going to go to...
05:42 Right-click Menu, import package, custom package, and we'll need to navigate out
05:51 to this video's exercise assets folder to load it.
05:59 And we're looking for robot 2. Import.
06:06 And there we've got him. Unity packages are different than
06:09 importing straight assets, because they contain things like scripts, prefabs and
06:13 all of the linkages that allow you to drop it straight into the scene, without
06:17 having to reset things up. Let's open this guy and take a look.
06:24 And to click on him, and then the Animations Section of the import settings.
06:29 We can see that he's already had his clips already set up.
06:33 So he's good to go. So I'm going to drag him into the scene now.
06:38 Put him next to his buddy. He does need the new test controller.
06:43 So we'll drag that onto him. And now if we click Play we'll be able to
06:49 see him behaving properly. So let's stop Play mode and delete the
06:54 original one from the scene and look at the next issue.
07:00 So the second issue is Deloris. She's lost her widespread stance.
07:07 And configure muscles we can add an offset to our legs with feet spacing.
07:12 So we need to select in the Project view. Go to Rig > Configure and yes, we want to
07:19 save the scene. And under muscles, at the bottom under
07:26 additional settings, we're going to set her feet spacing to 0.1.
07:34 This should spread them back out when she's in that idle position that she
07:37 inherited from some other character. And we need to click apply, and done.
07:45 Then I'm going to zoom out, and when we click Play, we should see her feet spread again.
07:57 And so, now she's standing better. The next issue is BoxMan.
08:03 Depending on where you set your camera, he may have completely disappeared from
08:07 the view. That's because at the start, he was out
08:10 of the view being frustum culled. The frustum is the frame section of the
08:14 world that the camera sees. On play, and his is animation, not his
08:18 transform that moved him back toward the rest of the characters are, so the camera
08:22 may not have bothered to draw him. If you move the scene back, he'll
08:28 magically appear in both again. So, there's good reason to make sure that
08:32 last animation was back at the starting point.
08:36 We'll take of that later on. The last thing we need to address is a
08:40 problem with the idle cycle. Sometimes, depending on your character
08:44 you may see the feet sliding underneath the character instead of the pelvis
08:48 shifting from side to side like it should.
08:52 This is because of the way Mecanim uses the hips or pelvis.
08:55 To fix it, and this is a general thing we should probably get in the habit of doing
08:59 for idle cycles, we need to head back to the animation section for the box man and
09:03 check Bake and Depose for the xz position.
09:08 In both idol clips. So this is the one we want right here,
09:12 and we'll do it for warm up which is an idol and also the regular idol.
09:18 And Apply. So Z is a forward motion we don't want to
09:22 check for the Bog Walker fast crawl. Basically, anything that's moving forward.
09:29 So now we've made a pretty good start at setting up our character with Mecanim.
09:34
Collapse this transcript
Adding a walk state to the controller
00:00 In the last video we started setting up the Animator Controller, but quickly
00:03 discovered a few issues with our characters once we had them in the scene
00:07 and animating. In this video we'll take a closer look at
00:11 the state we created in the Inspector. So lets get back to the animator here by
00:16 clicking on its tab. And we'll click on the idle state, and
00:20 now we can see it over in the inspector. At the top is its name, which we changed
00:25 to just idle. It's worth mentioning that when you're
00:29 changing the name of the state. You're not changing the name of the clip
00:34 it currently represents. The next place is for a tag is not
00:38 related to the regular game object tag, but it works pretty much the same.
00:45 In scripting, it can be used to see if the currently active state has a matching tag.
00:51 You could do the same with the name, but the tag allows you to be a bit more generic.
00:56 In the next section, the first parameter is speed.
00:59 This is where you can adjust the speed of the clip, remember that warm up clip that
01:04 was kind of slow? Well, this is where you'd adjust it,
01:08 faster is higher than the default 1, and slower is lower.
01:13 Next is the motion. This is where you load the clip.
01:17 Because we drag the clip into the view to create the state, the clip is pre-loaded
01:22 for us. If you create an empty new state, this is
01:26 where you would load the clip. You can do that in the usual ways, you
01:30 could drag a clip into the field, or use the browse button and select it from the list.
01:36 Let's take a look at browse. It lists all the clips that are currently available.
01:42 Right now, there aren't too many but you'll see the advantage of our naming
01:45 convention as the list gets bigger. Let's close it and look at the next
01:50 parameter, Foot IK. Foot IK prevents sliding in transitions
01:55 and blends as the character moves between animation clips.
02:01 It makes sure a particular foot in the new state comes in to match up with the
02:04 same foot in the old state. It does not align the foot with the
02:09 terrain he's walking over. That would take some serious vector math.
02:15 And ik parameters from the pro version. The next parameter is mirror, this
02:20 provides us with a means to mirror animation from left to right without even
02:24 having to generate an extra clip for it. So if you have a look right animation you
02:30 can turn it into a look left animation. >> Just by ticking the check box.
02:35 Next step is a transition list. This will show what transitions have been
02:40 set up for the state. Our idle state for example, will
02:43 eventually be allowed to transition into the bog walk state or any other state we
02:47 decide to set up. So let's go ahead and drag another clip
02:51 into the view. To make another state.
02:54 I'm going to grab the bog walk clip, and drag it into the animator view.
03:00 Note how it comes in gray. If you wanted to, you could make this one
03:05 the default state. Let's go ahead and make the bog walk the
03:09 new default state. We right click on top of it, select set
03:15 as default. The idle state changes to gray, and the
03:20 bog walk state is now the new default, orange.
03:23 To test it we'll need to click play. The four companions will march slowly out
03:28 of the view so you'll want to switch back to the scene view.
03:33 There you'll be able to see Linny and the Box Man slowly pulling away from the
03:37 other two, since they've got longer legs. So let's switch to the scene view.
03:42 Zoom out a bit, and click play. And we'll stop.
03:55 This would be a good clip to test the clip speed on, so let's go back to the
04:00 animator controller window, select our Bog Walk, and let's change its speed to
04:06 maybe 2. Now let's click play and watch the action.
04:12 I'm going to switch back over to scene mode again.
04:16 And you can see they're now double-timing it out of the view.
04:23 Before we finish here, let's go ahead and set the speed back to 1.
04:29 And in the animator let's go ahead and turn our idle into the default again.
04:35 And since we've made some changes let's be sure and save the scene.
04:43 So with this video you've had a first glimpse of what Mecanim is all about.
04:47 We've used animations from a single character on a variety of characters, and
04:51 Mecanim has adapted it for each thanks to the configuring process we went through earlier.
04:58
Collapse this transcript
Creating transitions
00:00 Previously, we created our first states and experimented with changing the
00:03 default state, the orange one. When we click Play, all of the characters
00:08 went into that state, and depending on which state we chose as the default, they
00:11 either stood around in the idle state or headed out of the view looking like
00:14 something out of Monty Python. In this video we'll start setting up the
00:20 transitions that will control both the blending between the animation clips and
00:24 the conditions that trigger them. Let's start with the idle to bog walk transition.
00:34 To do this, we right-click on the Idle State.
00:36 Select Make Transition, and as soon as the cursor is over our other state, click
00:42 to finish it. And since most states are going to
00:47 transition back out of it. We'll do this as well.
00:51 Right click, make transition, snaps over here, and click to finish it.
01:00 To set up the transition, we click on the one we want.
01:03 I'm going to click on the idle to bog walk, and you can see it turns blue.
01:09 >> And we get the information for that transition over in the inspector.
01:13 At the top of the inspector we see the transition, Ideal, to be BM_Bog walk.
01:19 Next to transition we've got check boxes that will allow us to perform tests in
01:23 the controller. Solo and mute.
01:27 We'll save those for later, but then can bale and disable the transition during testing.
01:32 Next we've got something called atomic. When checked atomic prevents the
01:37 transition from being interrupted by another transition until it has fully
01:42 played out... Now here comes the best part.
01:47 This is where you control the blend between the two animation states.
01:51 And I'm going to give my self some more space here.
01:54 The timeline is shown in seconds. An it shows the default transition.
02:02 This is the overlap section. You can move the markers to adjust the
02:06 length of the transition, but for now let's just leave it as is.
02:10 You're probably itching to see some action by now, so we'll skip over the
02:13 conditions section for a minute, an go ahead an click play in the preview.
02:19 I'll right-click to bring up the preview window again, an we'll click Play and Stop.
02:29 So we can see the transition between the idle state and the bog walk state.
02:34 Now let's head back up and look at the condition section.
02:39 As a default, it's set to exit time. The length of the previous state's
02:44 animation clip, except that you also need to factor in the transition time.
02:48 So the condition that triggers the walk state is 91% into the idle clip, or .91.
02:57 Let's try moving the left transition marker further left.
03:02 The value changes to reflect the new location on the idle clips timeline to
03:06 start the transition. When you move the marker back you'll need
03:10 to manually move the walk clip back, but it snaps quickly into place.
03:14 So with exit time. Even though we set the clip to loop exit
03:19 time will cause it to transition back into the walk state as soon as the clip
03:23 has reached the start of the blending or overlap time.
03:27 Let's check the transition back to the idle state.
03:30 This time the exit state is 0.88 seconds into the walk clip.
03:38 I you've gotten carried away with experimenting with the transitions on the
03:42 timeline you can zoom and pan it in the usual ways.
03:45 Middle mouse roller. Pans it and rolling it expands and
03:49 contracts it, and, of course you can always use the alternate keyboard and
03:54 mouse combinations. What the transition's running themselves,
03:59 we ought to be able to see some action when we click Play but to see the next
04:03 Mecanim features working You will want to have the game view zoomed out more, so
04:07 let's move over to the scene view and move our camera out.
04:14 That should give us a pretty good view. And then we want to switch back to the
04:19 animator view, and then we'll click Play. And the characters switch between the
04:28 idle state and the walk state as each state's exit time is finished.
04:34 And let's stop. Now here's a really useful thing.
04:41 And that is if we select one of the characters and I'm going to select Linyi
04:45 and then click Play. We'll be able to see each states progress
04:50 as it goes from the state through the transition and into the next state.
04:55 So let's click Play and watch what happens.
04:57 Remember Linyi selected this time. Also note that you can see Lenny's name
05:03 in the lower left of the animator view during play mode, since he's the
05:06 currently selected character. And we can see our state engine in action.
05:16 And let's stop. So now you've seen the most basic of
05:20 transitions and how it blends the animations from one state to the next.
05:25
Collapse this transcript
Setting up characters to interact with their environment
00:00 In a previous video, we set up simple transitions to and from the idle and walk
00:04 states, but we have no idea how they will react to things in their path.
00:09 Now's an ideal time to get the characters physically set up to react with the
00:13 scene, because they don't yet require any user input to get them moving.
00:18 So in this video we'll add a couple of components to the four characters, so
00:21 they can start interacting with their environment.
00:24 Let's look into a few critical bits of information in regard to the animator component.
00:30 The currently loaded Bog Walk clip has a forward velocity of 1.15 as we can see if
00:35 we checked on it in the inspector for the clip itself, but that doesn't necessarily
00:40 mean that it will be used. Let's select Lenny and in the inspector
00:47 uncheck Apply Root Motion.. This is a key concept in Mecanim, so I
00:53 want to make sure we see it early on. Let's click play and see what happens.
00:59 The other three characters stepped off smartly at the transition point, but
01:04 Lenny just walked in place, because we unchecked Apply Route Motion.
01:11 In other words, we didn't use the motion that the clip came in with.
01:16 Let's go ahead and turn it back on. Next, we'll want to see if the characters
01:20 are interacting with the ground. Rather then setting up a terrain, let's
01:24 go ahead and drop a cube in the scene and use it as step, or rather a low platform.
01:31 So I'll switch over to the scene view. Give myself a little space, and I'm going
01:38 to zoom in on one of the characters, and then from game object, create other.
01:45 I'll create a cube, and I want this one to be about .2 high, and then I'll scale
01:54 it out. And position it, so that the characters
02:05 will have to walk over it. The camera's already in a pretty good
02:14 position to get a view of it, so let's click Play and watch what happens.
02:24 It looks like they went through it, like it wasn't even there, but we know that
02:27 the cube has a collider, so the problem lies with the characters.
02:31 They need not only a collider, but a rigid body as well.
02:36 So let's start with Lenny, and add a capsule collider.
02:39 And a rigid body. Component > Physics > RigidBody.
02:44 Component > Physics > Capsule Collider. Now it's going to need some setup.
02:56 So the first thing I want to do is switch my view to a side view.
03:04 And make it a iso. Then I'm going to hide my cube, and zoom
03:10 in closer, select my Lenny again, I'm going to give the capsule collector a
03:17 height of two. And I need to give his y offset
03:27 approximately one. This, by the way, is why we wanted to set
03:32 up some of the characters with the standard two meter height.
03:37 Much easier to set up the capsule collider.
03:39 And it looks like maybe I can shrink his radius, a little bit as well.
03:47 That looks pretty good. The important thing to remember here is,
03:51 when we hit play, we're going to check to see if his feet are on the ground.
03:55 If they're too high off the ground, or too low into the ground, we're going to
03:59 change the capsule collider's y distance to adjust that height.
04:04 So let's click play and see what happens. As soon as he transitioned into the walk
04:13 state, he fell over flat on his face. So here's one of the most important parts
04:18 of the character setup. In the rigid body component, under
04:23 constraints you must set all three freeze rotations constraints to on.
04:29 So we're going to do that right now. Under constraints, freeze rotation x, y
04:35 and z. And let's go ahead and turn on the cube
04:41 to see how he interacts with it. Click Play.
04:49 And you can see this time, he stepped up onto it, and when he got to the end of
04:52 it, he stepped down off of it. Box man on the other hand is going to be
04:57 a problem. We'll deal with him in the next video.
05:01
Collapse this transcript
Solving problems with environmental interaction
00:00 In this video we'll take a serious look at Box Man and see what his setup issues are.
00:05 If we add a capsule collider to him, it's suddenly apparent that he is in his crawl
00:10 behavior at the end of his animation. Way off from his starting location.
00:15 Rather than trying to get the collider aligned remotely, lets just import a
00:20 better version of him. With the Legacy Animation system
00:24 characters always start at the scene in their first frames position.
00:28 With Mecanim however, they go into the scene in their final frame's position.
00:34 So now it becomes important to end the character in his starting position.
00:39 Even if the last few frames are never used in a clip.
00:43 And as long as we needed to replace the character, I've also included several
00:47 more animations that we'll need throughout the rest of the series.
00:51 So from the right click menu, once again, I'm going to Import Package > Custom Package.
00:57 Navigate my way out to my folder. We're going to start with the BoxMen.unitypackage.
01:10 Import. This one takes a little bit of time
01:15 because it's got a lot of animation clips with it.
01:18 And we also need the prefab for Box Man so we won't have to spend time setting
01:24 him up. JustBoxMan Prefab.
01:30 So JustBoxMan doesn't have any animation on him.
01:35 If we look in our new folder here. But now we have more animations on three
01:41 different versions of Box Man. And you can see there's a lot of new
01:47 animations there. The reason this is worth noting is that
01:51 with all of them together, updating the program whenever I made any changes in
01:55 the animations took way too long. So keep in mind that when you do make
02:00 your own animation clips you probably don't want to have more than about ten or
02:04 so per character. So let's go ahead and drag our JustBoxMan
02:10 Prefab into the scene, apply our test controller to him, and now we can delete
02:17 the original bad Box Man. And click Play.
02:28 And everyone but the poor little robot who's too short, go ahead and march over
02:32 the platform. So finally, with the addition of the
02:35 collider and rigid body components, we've got our characters reacting to the
02:40 environment nicely.
02:42
Collapse this transcript
Discovering input parameters
00:00 So far we've been able to do some basic setup on the characters in the scene,
00:04 while the transitions were still controlled by the length of their state's clips.
00:09 While using exit time as a condition that triggers the status useful for finite
00:13 states, most looping states need an outside trigger.
00:17 To use an outside trigger we need to make use of parameters.
00:22 Let's start by looking at the choices. I'm going to go back to my animator window.
00:30 And click on the plus for my parameters. We've got vector, float, ent and boul.
00:38 The most common condition will be when the player is using the input keys to
00:41 direct the character. But for a quick refresher on the input
00:45 key values, let's drop a first person controller into the scene.
00:50 And we can get him from the standard assets folder.
00:53 His input is monitored in the first person input controller script.
00:59 So let's open that up and add a print statement to see what kind of value the
01:03 forward and backward movement gets from inside the standard assets folder again.
01:09 I'm going to double click the FPS Input Controller and that will open up in the
01:14 scripting editor. Line 11 is where it takes the input from
01:19 the player. Input dot get access vertical is the one
01:23 we want. So I'm going to copy it, and just add a
01:29 print statement in here. So we can see what kind of values we're
01:37 dealing with. So we'll save the script and head back to
01:43 the editor. And then I'll need to disable the
01:48 first-person controller's camera, otherwise we'll get a message about it.
01:52 Now, when I drive around the scene, the console is going to tell me the value of
01:57 the vertical input, no matter which of the keys mapped to it I press.
02:03 And you'll see that the value is 0 when I'm standing still, and ramps up to 1 as
02:08 I press the Up arrow or the w key. So I'm pressing my key now, I let off.
02:17 Press it again. Let off.
02:19 And I'll stop now. The important thing to note here is that
02:25 the value is not an integer or whole number.
02:27 It's a float, a floating point number. So, a safe bet would be to say that
02:32 whenever the value of the vertical input is greater than 0.1, our character needs
02:36 to go into the walk state. So, the first parameter we'll need to add
02:42 is one for vertical input. An we'll name that V input Afloat.
02:49 So I'll open up my animator, click plus, select float, and we'll name this one V Input.
02:57 V for vertical. Unlike scripted variables, we can use
03:02 spaces and capitalization wherever we want.
03:08 Because we will refer to the parameters as strings.
03:11 For that reason, you'll need to be aware of leaving trailing spaces on the names.
03:16 You'll often see people use speed for the name of this variable, but I prefer to be
03:20 reminded that it's just a number indicating the value of the input device.
03:27 Before we delete the first person controller lets be sure and try going backwards.
03:32 That ramps into a negative 1. So I'll click play and this time I'll
03:38 drag my first person controller forward and backward, forward, backward.
03:49 So you can see the negative number, and we'll stop Play mode.
03:53 Okay. So now let's go ahead and delete the
03:55 first person controller. And then we'll start thinking about the
04:00 transition from the idle state into the walk state.
04:03
Collapse this transcript
Defining input parameters
00:00 When we left it the transition from the idle state to the walk state was using
00:04 the exit time. Now we're going to tell it to use the new
00:09 V input parameter, so let's go ahead and select it in the animator view.
00:16 When you click the conditions down arrow, you will now see the parameter we've just
00:20 added, V Input, and we're going to select it.
00:26 Now we get a new addition to the line. We get a drop down for the condition.
00:31 I'm going to give myself a little more space so we can read it better.
00:36 The drop down contains greater. Or less.
00:44 The default grader is good here, so we'll leave it, and add the .1 value in the
00:48 next field. Remember, this is the value of our input device.
00:57 So 0.1. So now, the walk state will be triggered
01:01 when v input is greater than 0.1. Let's set up the transition for the walk
01:08 back to the idle. Select it.
01:11 Change our conditions to V input, and this time we want less and less than 0.1.
01:26 At this point, there's nothing that assigns the value of the vertical input
01:29 to the V input parameter. We'll do that in scripting later.
01:34 But we can still test the parameter by changing the value manually.
01:38 Let's click Play and we'll be able to see that the characters no longer transition
01:42 into the walk state on their own. Next we'll be changing the V input value
01:47 to anything greater than 0.1 to get the walk cycle started again.
01:53 They will keep looping back into it because it will always be greater than 0.1.
01:58 Until we set the value back to zero. Let's try that.
02:05 And if we set it back to zero, they go back into idle.
02:12 So, now that we've got a couple of transitions set up, we'll finally be able
02:16 to see what the Mute check box does. We can access it from either the
02:22 transition or the state that the transition comes from.
02:28 So I'm going to select my transition into the Bogwalk, and I'm going to select Mute.
02:36 We see that the arrow here turns red indicating that it's muted.
02:39 Now when we click play and turn up the V input value, nothing's going to happen
02:46 until we unmute. So we'll click play, turn up the value
02:53 and as you can see, they're not walking. But as soon as I go over and uncheck
03:04 Mute, they start off walking again. Solo, the other option turns the one you
03:13 pick on and all others off. This one can set up ambiguities,
03:18 especially if you are also using Mute, so be careful when using it.
03:22 With the introduction of parameters in the conditions we've now had a peak at
03:27 the driving concept behind the Animator controller.
03:31
Collapse this transcript
Working with additional parameters
00:00 In this video, we'll look at a couple more parameter types and the logic behind
00:04 the choices as we stipulate multiple conditions.
00:08 We'll start with the condition that tells our character whether to go into the run
00:11 or the bog walk. This one will be controlled by a simple
00:15 two state variable. So let's make it a Bool for Boolean
00:21 parameter and name it Bog Walk, Bool. Just like the V Input parameter, it's got
00:30 a place to set its initial state, but this time it's a checkbox, and it starts
00:34 out as false or not checked. During the race, the character will be running.
00:41 So let's make a new state, using BB_Run, and name it run.
00:46 So I'm going to right-click, Create State.
00:51 This time I'm going to do it this way. I'm going to select Empty, and select my state.
00:58 And now, I'm going to browse, for, my asset, and we're looking BB_Run.
01:14 So this time, my motion is in here, but my state hasn't been auto named.
01:18 So let's name this Run. While I'm at it, I'm going to replace the
01:24 BM_Bog Walk with the BB_Bog Walk, and I'm also going to shorten the name.
01:34 So Browse, and this time, instead of BM, the original box man, we're going to
01:43 choose BB_Bog Walk. This way I can delete my original Box Man
01:50 and clean up some of the extra clips in here.
01:53 So now, we need to make the transitions between Idle, Run and Bog Walk.
01:58 From any one of the states, you should be able to get into the other.
02:03 So I'm going to tidy up my workspace here.
02:11 And I wanted to put my Run up by my Bog Walk.
02:19 And then we'll make the transitions. This time it's going to need two conditions.
02:25 V Input needs to be greater than 1. And, this time, Bog Walk will equal true.
02:31 Instead of changing it here, I need to add a new one.
02:37 So I'm going to hit Plus. Now, I can go in here, click Bog Walk.
02:43 And it starts out as true as the default, so we're good to go on this guy.
02:47 For Idle to Run, we need V Input greater than 0.1, so we'll create a transition.
02:56 Select it. We need VI nput greater than point 1.
03:08 And, we need Bog Walk equal to false. So we'll add another condition, and this
03:18 one is going to be Bog Walk, false. From the Run back to the Idle, we only
03:30 need V Input less than 0.1. From Run to Bog Walk, we need this
03:39 transition, to be Bog Walk equals true, because, as you're already in Run, you're
03:47 already greater than 0.1. So Bog Walk equals true is all we need there.
03:59 And, for our transition from Bog Walk to Run, we need Bog Walk equal to false.
04:13 The only one we haven't checked on, is Bog Walk down to Idle, and it's still
04:19 good as it originally was. So let's take a breather and see how
04:28 these transitions work when we manually change V Input and Bog Walk.
04:36 So we'll start by turning this to 0. And we should probably stop the scene and
04:41 start at 0, so we can see them. So V Input equals 0, play, so we're in Idle.
04:51 Now, if we go to Bog Walk, and change this to 1, they start into their Bog
04:56 Walk, but as soon as we uncheck it, they pick up and keep running.
05:05 And if we were to change the 1 back to 0, they'd be idling somewhere off in the distance.
05:13 So let's click play and stop. In case you're wondering, with Foot IK,
05:16 Mecanim keeps track of the feet location and does it's best to make sure the same
05:21 foot is forward in the new clip, as was forward in the old.
05:28 That's why the progress bar during a transition may start up halfway along.
05:32 Let's make things more interesting now. If the player is not racing, he can only walk.
05:40 So, let's make a walk state with the BB_Walk clip.
05:43 I'll right click Create State > Empty. We'll name this one Walk.
05:54 And bring in the BB_Walk clip from the Asset section.
06:03 And we'll make a transition from Idle to Walk and Walk to Idle.
06:13 The transitions we'll use, the V Input greater that 0.1.
06:19 For the n and V Input less than 0.1. For the transition back to Idle.
06:34 But now, we have a problem, both the Idle and the Run use V input greater than 0.1.
06:43 So it looks like we'll need an additional parameter.
06:45 We could use Racing, a Boolean equal to true or false.
06:50 But if we think ahead, we can incorporate the logic needed to run the starting
06:54 sequence at the same time. Setting up a race state sequence will
07:00 give us a chance to use an Int type parameter, rather than create a Boolean
07:05 for each of the three starting states, Not Racing, On the Mark, Set to Go, and Racing.
07:14 We'll use an integer, 0 will be Not Racing, 1 is On the Mark, 2 is Set to Go,
07:19 and 3 is Racing. So let's create an Int type and name it
07:26 Race State. Now, we can add Race State equals 0 to
07:32 the Idle to Walk transition. And here is our new Race State.
07:40 And, as long as it's 0, our character only gets to walk.
07:46 We also need our V Input, which I just overwrote, so I'm going to add one more
07:53 again, use V Input, greater than 0.1. For the transition out, V Input needs to
08:03 be less than 0.1, so we're good to go on that.
08:09 And for our other two states, Bog Walk and Run, the end transitions also have to
08:16 stipulate that Race State, is going to be greater than, and notice we have a lot
08:23 more choices when we're using integers, greater than 2.
08:33 Go over to the bog walk transition, add another condition.
08:38 Race State greater than 2 again. And the return transitions are okay as
08:49 they are. If you decide to click play and test this
08:53 now, be aware that there is no transition from Walk to Run if you decide to change
08:58 the Race State to 3. So now, we've seen that with multiple
09:03 conditions, we found we could have some extra control over the transitions, and
09:07 how they were operated.
09:09
Collapse this transcript
3. Scripting the Controls
Scripting player input
00:00 In this chapter, we'll be creating a script that manages the perimeters and
00:04 tells Mecanim state engine when they've changed.
00:08 Now, by the way is also a good time to start organizing our Mecanim assets.
00:12 So, let's create a folder for them and name it Mecanim Assets.
00:16 Create folder. And inside this folder, I want to create
00:25 a couple more controllers and scripts. And then we'll start by creating a script
00:37 in our new scripts folder, so I'm going to Right click again and we're
00:42 going to be using Java script, and we'll name our first one Basic Character control.
00:55 Then I'll go ahead and double click it or you could also press Open in the
00:59 Inspector to open it in the script editor.
01:02 The first thing we'll need to access is the animator component, so we'll start
01:06 with the variable to hold that. So I'll just head out to the snippets
01:12 file for the code and I'll copy and paste my first line.
01:17 Ctrl+C and then I'm going to paste it up near the top.
01:26 So this one is an internal var named animator, which is going to find the
01:30 animator component. And that's what we're going to store there.
01:35 In the start function, we'll go ahead and assign it.
01:38 So I'll head out to my snipits file again and grab the next bit we need because you
01:44 don't want to watch me sit and type. Ctrl+C for copy, and we're going to put
01:52 that into our start function. And if you want to tidy up you're more
01:58 than welcome to. So this one we're going to use get
02:01 component and we're looking for the animator component and then we assign it
02:06 to the animator variable. And since our first scripts will go
02:11 directly on the character, we only need to locate just the component not the
02:14 object itself. So let's get our simple navigation
02:18 working first. For that we're going to need a couple of
02:21 variables to hold the current values of both the horizontal and vertical inputs.
02:26 So I'll head out and grab those as well. Ctrl+C and then we'll paste them in here, Ctrl+V.
02:39 And so now we've got h, which is going to be holding the horizontal input value,
02:45 and v which is going to be holding the vertical input value and we'll go ahead
02:50 and keep them public so that we can watch them in the Inspector.
02:59 Now we need to assign the input values. Remember they're going to range from
03:03 minus 1 to 1, and this is done in the update function where it's updated every
03:07 frame, so I'll head out and grab those lines.
03:15 Ctrl+C and in the update function, give myself a little bit of space.
03:23 Ctrl+V, and I'm going to Right Click and Indent.
03:31 So here's where we're using the get access horizontal, this is what we get
03:35 for the player's input and a vertical is right here as well.
03:42 So let's go ahead and save the script and drop it on each of our characters.
03:48 Save. We'll get back to our editor.
03:56 Then I'm just going to drag and drop it from the Project View onto each of the
04:03 characters and the robot. If we click Play now, nothing will happen
04:13 because we still need to tell the animator controller where the values are.
04:18 To do that, we're going to need to set the value of the parameters directly in
04:22 the animator. We'll be doing that in a fixed update
04:27 function, so the value is reported at a constant rate.
04:33 I'm going to head out to my snippets file again and grab the last bit of code.
04:40 Ctrl+C, and in the script editor, give myself a little bit of space after the
04:45 update function, and Ctrl+V to paste that in.
04:51 So right now we're only going to be reporting the V input.
04:54 But when we do that, we should be able to see it in the animator's parameter section.
05:01 So we'll Save the script. Head back to the editor and see what happens.
05:08 This time when I click Play and use the W or Up Arrow we should see the characters
05:13 move forward and we'll also see the V input value change as I do that, so let's
05:18 click Play. I'll press my up arrow, and they start
05:27 walking forward. If I let up, it drops back down.
05:31 Now we don't see anything in the input value here, because I don't have a
05:36 particular character selected. So I'm going to go ahead and select
05:42 Lenny, and now we can see that. Activate my game view and click the Up
05:48 arrow again, and let it go, and you can see that it's working as we expect.
05:57 So I'm going to stop play mode. The walk feels awfully slow, so to speed
06:01 it up since the clip's velocity controls the speed, I need to set it on the walk
06:06 state itself. So I'm going to select the walk state and
06:14 let's give it maybe 1.5. Now I've stopped Play mode but if you
06:20 were to set this during Play mode, you'd also find out that it would be retained
06:23 unlike setting a lot of parameters in the inspector when you're in Play mode.
06:29 Before I click Play and test this because we have changed the speed before, I'm
06:34 going to do one other thing. That is, we can't turn the characters
06:39 yet, but we can make a walk backward state out of what we've already got at hand.
06:44 So let's right click over the walk state and select Copy.
06:48 And right Click and Paste, now we've got something named walk zero.
06:57 Let's rename that to Walk Backwards. And instead of loading a new clip, all we
07:04 need to do is change the speed to a negative number.
07:10 Now, 1.5 might be a little fast for a backwards walk, so I'm going to use
07:16 negative one. We'll create the transitions next.
07:22 They use the V input parameter. So I'll right-click from idle, Make Transition.
07:29 Right-click from backwards and make this transition from idle into walk backwards,
07:36 this time we're still using v input, but guess what?
07:43 It's a negative value, so now we want to use less than minus point one.
07:52 That puts us into backwards. To get back into idle, our transition is
07:59 going to be V input greater than minus 0.01 again.
08:06 We should also make sure that he can only go backwards when he's not in the race state.
08:11 So, I'm going to add an extra parameter on my walk backwards at the plus.
08:17 I'm going to make sure that he also has to be in race state 0.
08:23 In other words, not racing. So let's go ahead and click Play and Test.
08:29 I'll be using my input keys. He's in idle now.
08:35 I click the Up Arrow, they go forward. If I stop, they stop.
08:42 If I click the Down Arrow, they go backwards.
08:45 And I'll let up on my key and so we can see that everything's working.
08:49 So let's stop. You're probably thinking it would be nice
08:52 if the character turned his head occasionally to see where he's going
08:56 while he's walking backwards. You'll be able to see how to do that when
09:01 we get into masking. So in this video, we've seen not only how
09:06 we can tie the animator controller into the scripting, but also how we can use a
09:10 negative value to run a clip, literally backwards.
09:15
Collapse this transcript
Turning the character
00:00 We made a start on controlling the characters when we were able to trigger
00:03 the walk state and its animation. That animation included forward movement,
00:08 so Mecanim was respospisble for actually moving the character in the scene.
00:14 Turns are a bit trickier if the character is walking or jogging other then having
00:17 them look the direction he's going we really don't need a separate animation.
00:22 If he's running at speed and leaning heavily into the turn like in the unity
00:26 demos he'd want to use a blend tree we'll be seeing those later on in the course.
00:34 When a real person is turning in place, he will be looking where he's going but
00:38 once his head is facing his goal, he stops turning and then his feet and body
00:43 have to finish up. In games, the problem is that we have no
00:48 way of knowing just when the player is going to let up on the Turn key.
00:53 So we can't anticipate when to start the head transition to stright ahead.
00:58 So the bottom line is, for this simple set up, we won't be controling the turns
01:03 with mecinum using the clips angular velocity, at least not at this time.
01:09 We'll be doing it with code. So, let's start by opening the Basic
01:13 Character Control Script we started in the last video.
01:17 First, we'll need a variable for the rotation speed.
01:19 So, let's open our script, Basic Character Control, and I'm going to
01:24 double-click on it. Then I'll tab over to my snippets file,
01:30 and grab that variable. Ctrl+C to copy, Tab to our script editor,
01:36 and I'm going to add this one underneath the others.
01:41 So this is a rotation speed that we'll be able to adjust to get just the speed we want.
01:48 Next, we need to add the code that uses it in the Fixed Update function to make
01:52 sure it's the same on slow as well as fast machines.
01:56 So I'll tab over to my snippets again, and we'll grab this line.
02:02 This one's a bit more complicated. Copy, and this one we need to add in the
02:07 fixed update, so I'm going to put it under this line.
02:14 So this is basically how we turn an object of any kind in a scene in unity.
02:23 Transform.Rotate, Vector3 is a three part variable like XYZ.
02:31 The x-direction, it's not turning yet. The y-direction, which is up, in the
02:36 unity world space is going to use h, which is the value of our horizontal
02:41 input, and then times dot Delta time which divides it into seconds.
02:47 So this will be that much time per second and then times our rotation speed, and
02:52 then there's no rotation in the z-direction.
02:56 Let's go ahead and sad our script and test it.
02:58 Click Save, tab back to the editor. If we click Play and start walking the
03:05 characters, then press the turn keys, which are the arrow keys.
03:12 While they're still moving forward, it's quite passable.
03:14 I'm going to click Play, walk the characters forward and then press one of
03:18 my Arrow keys and you'll see that they turn nicely.
03:23 I'll press the other one, and they turn the other direction.
03:27 So we know it's working. And I can let up now.
03:32 Now we need to check our turn in place, and for that I think I want to switch to
03:37 my scene view and zoom in on the characters.
03:41 So I'm going to click Play, leave them in idle, and use my turn keys.
03:50 And you can see the characters turn, but we have absolutely no animation to go
03:55 with it, other than what they're already getting from the Idle animation.
04:00 So let's stop Play mode and do something about it.
04:05 The first thing we need is a float parameter.
04:08 So we can use the horizontal axis input. Remember that value is going to be minus
04:14 one to one. So let's go back over to the animator,
04:18 add a new parameter. One to minus one is a float.
04:25 We're going to name this one turning. Then we'll need a new state So from the
04:32 right click menu, I'm going to create state, empty, select it.
04:38 And I'll call this one turn in place. I don't want to load the BB Walk in place
04:46 into it. So I'm going to browse, click on assets.
04:52 And look for BB walk in place. I'll double click it in.
04:59 For the transitinos, since floats don't have an equals or not equals operator,
05:03 you will need to account for two conditions to trigger the animation on or off.
05:10 One for each direction. So in the transition back to idle we will
05:16 need less than 0.1 and greater than minus 0.1, both using our turning parameter, so
05:23 turning is going to be less than 0.1. And another one for greater than minus 0.1.
05:42 But in the transition into the turn in place, we need greater than 0.1 or less
05:48 than minus 0.1. So in this case, we actually need to
05:54 separate transitions into the turn state. So we'll make our transition from idle
06:01 into turn in place. And the first transition is going to be turning.
06:09 Greater than 0.1 and then we need to make the second transition the same way we
06:14 made the first. So Right click, make transition, turn in place.
06:20 Now you can see we've got triple arrows to show that there's more than one
06:26 transition here. When we click on it, we now see our two
06:31 transitions up here in the transitions list.
06:36 When you're doing this, make sure that you click on the right one.
06:40 Here's the one that we've already set up. Turning greater than 0.1 and we need to
06:45 select this one and set this to turning less than 0.1 which is basically it's
06:50 turning the other direction. And that'll be a minus 0.1 as well.
06:57 And finally we need to get our script reporting the horizontal axis input
07:02 values back into the animator controller. So let's open up the script again and add
07:09 the v axis's h counterpart in the fixed update function.
07:13 I'll go out to my snippets for that one as well.
07:19 Ctrl+C to copy, Tab over to the script editor, and we're going to add that one,
07:26 it could go into this one, but I'm going to put it at the bottom anyway, and Paste.
07:36 So this guy is reporting the h value. Back to the turning parameter in the
07:41 animator, by using set flow. Because this is a flow variable.
07:47 We'll save the script, get back to the editor.
07:53 And since the clip is not driving the turn, you're probably going to need to
07:56 manually adjust its speed to match the rotation speed.
08:01 And now I've decided that about one point two on that turn in place for its speed
08:07 is going to match the value that we said in the script.
08:14 So, I'm going to switch back over to my scene view so that my characters up close
08:19 and click Play and now I'll try my arrow keys and now we can see our character
08:24 walking As, they turn, with the arrow keys.
08:30 And we can stop playback. It works okay but if you tried to go
08:34 forward from the turn. Because it has to transition into the
08:39 idle first, there's a little bit of a pause.
08:42 It would really be smoother if we created a transition directly into the walk and run.
08:48 That one's a simple v input greater than 0.1.
08:52 But we also need to include the race state so it knows when to go into the
08:56 correct date. If we wanted to have a turn in place
08:59 animation drive the turn from idle but not from the other gates.
09:03 We'd need a way to tell the script what state was currently active.
09:06 We'll see how to get that information in a later video.
09:09 One of the mechanim demo scenes has a very nice turn and place that uses an
09:13 extremely complicated blend tree to accomplish the feat.
09:18 The only problem is it relies on you to tell it ahead of time just how many
09:21 degrees to turn the character. Our solution may not be elegant, but it
09:26 gets the job done by controlling motion through scripting instead of the clip itself.
09:31
Collapse this transcript
Orchestrating a jump
00:00 With the basics of our character control taken care of, it's time to look into a
00:03 mainstay of game characters, the jump. So let's add a jump state to the animator.
00:09 Right-click, create state, Empty. And we want to name this guy Jump.
00:20 And we're going to load, bbfulljump into it, and we can go ahead and make our
00:26 transitions, right-click, make transition, right-click, make transition.
00:35 Now, I know this jump is a little bit on the slow side, so I'm going to go ahead
00:39 and move its speed up to 2. Let's go ahead and see what we've got
00:46 with the jump input. From the Edit menu, Project Settings,
00:50 Input, we can see that the jump is going to use the space bar, and then it's
00:55 looking for a key or mouse button. So now we know what to look for.
01:03 Then next we'll need to see what kind of values it produces and we'll do that in
01:06 the basic character control script. So let's go ahead and open that up.
01:12 We'll be triggering the jump from the script so we'll need to start by adding a
01:15 variable for the jump and I'll go out and grab that from the snippets file.
01:20 Copy and then we'll paste it back into the script, Ctrl+V.
01:28 We need to assign it and have it print out in the update function, so I'll head
01:33 back out to my snippets again. Grab our next line, copy, and we want
01:42 this one in the update function. And I'll just indent.
01:49 So now we can save this, head back to the Editor, and click Play.
01:57 And we are going to be watching the console line when we press the space bar
02:04 to see what prints out. So I'm going to click play, press the
02:12 space bar, press it again, and you can see that it's either 0 or 1, so we'll go
02:18 ahead and stop. So let's head back o the script, and
02:24 instead of having a float here, we may as well have an integer since it's only 0 or 1.
02:30 So we'll change it to type int and we should be good.
02:35 We could also comment out the print statement.
02:40 And save. Back in the Editor, we need to go ahead
02:44 and set up a parameter. To trigger our jump, so I'm going to
02:49 click this, and we're going to be using an int, and we'll call this one jump, and
02:54 now we can change the transition into the jump to use this new parameter instead of
02:58 exit time. So I'll select that transition into it
03:04 and change it to jump. Equals 1.
03:10 To fire it off, we're going to need to have the Script tell the Animator when
03:13 the key is pressed. So back in the Script Editor, under the
03:18 other two Animator commands, we'll add this next two lines, copy, and that's in
03:24 the fixed update, and paste. One of the difference is that in this
03:32 line of code is that we're using an int, but instead of setint like you might
03:37 accept, we have to use the full word. Integer here.
03:43 So be aware of that one. So we'll save this script.
03:47 One of the differences in this line of code is that we're using an int, but
03:51 instead of set int, like you might accept, we have to use the full word,
03:55 integer, here. So be aware of this one Now lets head
04:00 back to the editor. So now if we click play and press the
04:04 space bar, the character should jump. Play, Spacebar, Spacebar again, and there
04:11 they are, jumping away. If you're curious about what happens if
04:16 the space bar is left down, try it. We'll click play.
04:20 This time I'll hold my space bar down. And as you can see, the jump transitions
04:28 were completed before being retriggered. That happened repeatedly.
04:32 So for the jump, holding the space bar down should really either only trigger
04:36 the first time or allow the character to stay aloft a bit longer.
04:41 We'll be fixing that next.
04:43
Collapse this transcript
Refining jumps
00:00 In our last video, we left our jump in an ambiguous state.
00:04 For the jump holding the space bar down, should either trigger the first time, or
00:07 allow the character to stay aloft a bit longer.
00:10 To make that second scenario work, we can just switch the condition for the
00:14 transition from the exit time to using the jump variable with the value of zero.
00:21 So let's head back and do that. So our transition from jump to idle
00:25 instead of being exit time is going to be jump equals zero.
00:29 And zero's going to happen remember when the player lets up on the space bar.
00:38 But we're also going to need to change the jump clip from a full jump, to just
00:41 the top part of the jump. So let's change that from full jump to
00:49 just jump. Its speed is okay, so I'm going to set
00:53 the speed back to one. Let's try it.
00:54 Click Play. Press the space bar.
01:01 Press the space bar and hold. And then when we let go, the characters
01:05 slowly float back down. If our jump was higher it would be even
01:10 more apparent. So what we eventually need to do is get
01:14 physics involved. For now though, lets set the transition
01:18 back to exit time, and go with the first scenario where the player can only
01:22 trigger the jump once. So I'll set it back to exit time.
01:28 So now the challenge is to prevent a heavy handed player from triggering
01:32 multiple jumps. There are several ways to approach the
01:37 problem, so we'll just go with the typical one.
01:41 Let's go ahead and delete the jump parameter and make a Gool type parameter
01:45 named jump. So we'll delete it, add a new boolean
01:50 type Gool /g, and name it Jump. And we need to set our transition into
01:56 the jump, to jump equals true. And in case you're wondering why we
02:02 didn't use a Boolean type to begin with, remember we're using the input from the
02:08 jump, which was either zero or one. That's why we used an integer earlier.
02:16 So now we need to head back to the script, and add a flag for jumping and
02:19 initialize it to true. For my snippets file I'm going to get my
02:25 variable, and we're going to name this one can Jump, Ctrl+C for copy.
02:35 Ctrl+V paste, and then the fixed update we're going to replace this line animator
02:41 .SetInteger jump with a simple process jump.
02:47 I'm going to go ahead and comment this out, so you can remember what it was before.
02:56 So we're going to make a function called process jump.
03:01 And that's a little bit more typing, so I'm going to head out to my snippets file
03:05 again, and grab the whole function, Ctrl+copy.
03:09 So now I'm going to make sure I'm down below my fixed update.
03:17 And paste in the Process Jump function. So if we look at it, our conditional
03:24 says, if the space bar is down, j equals one.
03:27 And, can jump is true, we'll go ahead and trigger the jump.
03:33 Then we'll weigh the frame, then we'll set the jump parameter back to false so
03:37 it can't retrigger again. And also the can jump variable needs to
03:41 be set to false, so it won't let it trigger if it comes through again.
03:45 And finally, if the player lets up on the space bar, so j is equal to zero, we go
03:50 ahead an set the can jump variable back to true.
03:55 So let's go ahead and save our script. An head back to the Editor, an click Play.
04:05 An try holding the jump bar down, to see what happens.
04:12 So I'm holding it down, they jump once. And I'm still holding it down.
04:15 They aren't doing anything. I let up, and then I press down, and they
04:19 jump again. So our simple jump in place is behaving a
04:23 bit better, thanks to some extra scripting and a bit more logic.
04:28
Collapse this transcript
Using state information to add physics to the jump
00:00 In an earlier video, we set up a simple little jump in place.
00:04 It was a start, but could use more attention.
00:06 The first thing we'll need to do is check up on the collider, so let's turn on
00:10 gizmos in the game view and select the Linny character in the hierarchy view.
00:17 If we hit Play and hit the space bar we'll be able to see that the characters
00:22 colliders are not moving, so I'm going to click Play, and if I hit the space bar we
00:26 can see that the characters jump, but the collider, at least the one showing on
00:31 Lenny, doesn't move. So I'll go ahead and stop Play.
00:38 This means that we won't be able to jump the characters over anything, because
00:42 their colliders will stop them. Later in the series, you'll see how to
00:46 animate the collider parameters with Unity Pro N Curves.
00:50 But meanwhile, we can add a physics force to move the character, and its collider up.
00:57 The force needs to be added in the fixed updates function, so we're going to head
01:01 out to our basic character control script.
01:05 And I'm going to grab the script from my snippets file.
01:10 Ctrl+copy, and then the fixed update function, just above the call to process
01:16 the jump, we're going to add our new line.
01:21 So this means that when we press the space bar if it's okay to jump, if can
01:25 jump is true. And the player has pressed the space bar,
01:29 we're going to add a physics force. And I've set it to 200 but you may want
01:33 to play with that number, so let's go ahead and save the script and head back
01:37 to the editor. Let's select the characters again.
01:43 Or just Lenny, it doesn't matter which one or two or whatever, hit Play, and
01:47 this time when we press the space bar you can see that the collider and the
01:51 character goes upward. It works great until you hit the space
01:57 bar several times in a row. So let's try that.
02:04 And, I'll stop play mode. So you can see that the physics force
02:10 happily jacks the characters up higher and higher.
02:13 So we're going to need a way to prevent the ad force from happening again ,until
02:17 after the transition back to Idle is finished, this is another of those things
02:21 that can be solved in a variety of ways. But I'm going with the solution that
02:27 doesn't require heavy math skills. Logically we just need to know when the
02:30 character's back on the ground. Or precisely when his collider has had a
02:35 collision with it. You can see that in the little test
02:39 scene, we'll also need to include other objects like the platform.
02:43 So as a start, we'll take any collision. To check with that, we'll going to need
02:47 an in collision interfunction with the collison argument that returns useful information.
02:54 So let's head back to the script. First, we need a variable to tell us if
02:59 the character is in the air, or grounded. I'm going to go out to my snippets file
03:05 and get it, Copy+Ctrl+ c. And it's a variable, so I'm going to put
03:12 it right underneath canJump, Ctrl+v to paste.
03:17 Now we need to add the grounded variable to the add force conditional.
03:22 So let's get that one. I'm going to replace the whole line, but
03:27 basically we're just adding the n grounded part of this.
03:32 Ctrl-C to copy. And we'll replace this line.
03:39 Ctrl-V. So this is basically the part that got added.
03:44 So now, three conditions need to be met before the character can jump.
03:50 He has to be on the ground. The space bar had to have been let up
03:54 since the last jump, and now it has to be pressed again.
03:59 So the grounded variable also has to be added to the process jump function.
04:04 And this time I'll go ahead and copy my little bit extra here, Ctrl+C and we'll
04:12 add it in here, Ctrl+V. And we'll need to set the grounded
04:19 variable to false as soon as the jump is started, just above the yield.
04:24 I've got to snip it. So we can have the comments there.
04:30 Ctrl+copy and Ctrl+paste. And finally, we need an on collision
04:37 enter function, so we know when the character hits the ground.
04:43 I'll go and get that one. Ctrl+copy, that one goes below our last
04:50 curly bracket, paste. And this one's pretty simple all it does
04:57 is on the collision, and it's just the characters collider by the way.
05:03 On any collision right now, we're going to set grounded to true.
05:06 So let's go ahead and save the script and test it.
05:10 Back to the Editor, and I'm going to scroll my Inspector, so we can see our
05:14 basic character control, and we'll be able to watch our grounded variable change.
05:22 Now I'll just select Lenny for this one. So if I click play.
05:29 Press the jump variable. Watching the grounded over on the other side.
05:38 And you can see that every time he comes back down, grounded get's checked as true.
05:42 So we'll click Stop. So now there's still one more thing to solve.
05:49 In the animator, the jump only get's triggered from the idle state.
05:53 So if we were to drive the character around and press the space bar, he would
05:57 still jump, or at least he goes upward, but he doesn't have any jump animation.
06:04 So we're good from the idle, but we'll need to prevent the physics jump elsewhere.
06:09 So, we will need to get information from the Animator to know what state it's in
06:13 or transitioning to, to be able to control the add force.
06:18 And, to do that we need a special variable of type Animator State Info.
06:23 So, let's head back to our script. So I'm going to head out to my snippets
06:30 file again, and grab this guy here, copy and I want to add this one inside the
06:39 fixed update function. So, I'm going to make some space, and
06:48 paste it in. So, this one is a variable, and it's a
06:52 local variable since we're inside the function of type animator state info.
07:00 And we can find it, we already know who the animator is, we already identified
07:06 that earlier in the script. And this one uses get current animator
07:12 state info. And what it's going to do is get us
07:16 information on the current state or, if we're a transition, the state that it's
07:20 going into. Now,we can't ask for that name directly,
07:24 but we can give it a name and see if there's a match.
07:28 So now, we're going to need to wrap the add force.
07:31 With a conditional that checks the state name.
07:34 So I'm going to replace that whole thing. Going out to my snippets, an I'll grab
07:37 our next chunk of code. Ctrl+Copy.
07:49 See if I can find it here. We're looking for the add force.
07:52 Oh, here it is. But we're going to replace this whole
07:54 part here. And, Ctrl+V to paste.
07:57 I'll indent it. And let's look at what we've got.
08:03 So what we're doing is we're getting the state info is name, which will return the
08:08 name, but we can't see it directly, so we're checking to see if that matches
08:12 what we've given it. So we're going to say if we are in the
08:18 base layer Idle State, then we can go ahead and do all this stuff.
08:24 Otherwise, it won't. So let's go ahead and save the script and
08:27 test it. You get back to my editor.
08:32 So remember now, it's going to query the state before it allows either the jump or
08:37 the physics push. I'll go ahead and select Lenny just so
08:41 we'll see a collider again. Click Play, and if I press the space bar
08:47 at idle, they jump, but if I drive forward and press the space bar, and I'm
08:52 doing it several times here, nothing happens.
08:58 So, with the use of the idle state information we can now have a lot more
09:03 control over when the states get triggered.
09:07
Collapse this transcript
Using the Any state
00:00 In this video we'll be investigating the Any State.
00:03 We'll being using one of the clips from the better box man, the bb out of breath
00:07 clip, since our character could need to stop and catch his breath, at well anytime.
00:13 So let's start by moving the any state over near the idle state, and then we
00:17 need to create a new state, and load the out of breath clip into it.
00:23 So I'll right click, Create State Empty, load the out of breath clip into it, pb
00:28 out of breath. And we'll name our new state Catching Breath.
00:40 And then we can make the transition from any state to Catching Breath.
00:44 Right click, make trasition, click to drop it, but we can't make one back to it.
00:52 If I try, Make Rransition, and I click and it's not happening.
00:59 So I'll cancel out of that. That's because it can't trigger itself
01:05 from itself. So let's go ahead and see what the
01:08 transition we were able to make looks like.
01:11 Click on it, and we've got a new parameter that we haven't seen before.
01:16 It's called Preview Source State. And that's set to the default state idle.
01:21 If we check the Preview. And I'm going to right click to float the
01:25 Preview window. And click Play.
01:28 We can see that the transition to the catching breath state, is probably a
01:33 little slow. So, I'm going to speed it up a little by
01:38 moving the marker to the left. Now it turns out that since this is any
01:43 state, that means that this transition can happen into the catching breath
01:48 state, from any other state. So, just where do we find them?
01:53 Well, guess what? It's in this Preview Source State, and if
01:56 we click the drop down, we can see that all the other states we've got are listed
02:00 in it. So, at any time our character could be
02:04 triggered to go into this catching breath.
02:08 And we could go through and we could adjust those transition times as well,
02:10 but I think we can pass on that for now. So now we're going to need a condition to
02:15 set it off. And I'm going to make a new parameter, of
02:21 Boolean type and let's name this one Winded.
02:26 So we need our condition over in our transition to be winded equals true.
02:32 And if we check on any of the other states in here.
02:38 You'll see that it's using the same exact condition.
02:41 So let's zoom the main camera so we can see our characters better.
02:46 I'll go ahead and close the preview window.
02:49 Maybe I'll zoom in a bit. Select my main camera.
02:52 Use a line with view. And this is one of the times when you
02:59 definitely need to select one of the characters in the Hierarchy view.
03:02 Otherwise you won't see anything useful. I'm going to switch back to the animator here.
03:11 So now if we click Play, and then check Winded.
03:16 We see our Lenny character bend over. But the problem is, that it's continually
03:20 going back in to that catching breath state.
03:24 So we're going to need to do something with that.
03:27 Let's stop Play mode. The Any State parameter Winded, needs to
03:31 be a one-shot deal, and that'll need to be handled in code.
03:36 So if we click Play again and this time, click the Winded parameter on then off
03:40 immediately, we'll see our character go into the out of breath state properly,
03:44 but the problem is it's going to keep looping.
03:49 Let's try that. Click Play, click Winded, and click it
03:54 off again. And now we can see our catching breath
03:58 state is fully looping, so let's stop playback and figure out how we're
04:02 going to get him out of it... Since the Any State was not meant to be a
04:08 Final State, we need to give a transition back to something else.
04:12 If you had an Any State going into a Dying State, you'd want to transition it
04:17 into a Dead State or something else. For our little test, we can just have it
04:22 go back to the Idle State. So let's make the transition back to idle.
04:28 And leave it on exit time. Right click, make transitiion, and we'll
04:33 send them back to idle. And we'll leave our transition on the
04:38 exit time. It might be better to use another
04:41 parameter to use another parameter for the transition into idle just for testing purposes.
04:48 You might try something like race date equals five.
04:49 You can try it if you wish, but that will give you enough time to see that it will
04:53 need to be scripted to be solved. So there's one last thing before we head
04:59 off to do the scripting. During the regular game-play the player
05:03 might have a walk or run key down during the Catching Breath State.
05:07 So this would be a good time to allow the transition to idle to be interrupted.
05:13 So let's go ahead an turn off atomic for our transition back to idle.
05:17 Make sure we're in idle, an we'll un-check atomic.
05:22 So, that way if the player has his run key down or something, he won't have to
05:25 wait all the way through the transition to idle before he's off and running again.
05:30 So, now we're ready to finish the Any State functionality with scripting.
05:35
Collapse this transcript
Scripting the Any state
00:00 Our any state recipient, catching breathe, needs to be controlled by scripting.
00:05 To get it hooked up by our script, we'll need to make another parameter to manage it.
00:09 We have two choices here. We need one we can turn on then off to
00:14 start the state, and we need another to exit the state.
00:19 These can both be boolean types. Or, we could use an int type and have
00:23 three states. Zero, not out of breathe, 1, going into
00:28 the out of breath state and 2, in the out of breath state.
00:34 Since we're already getting pretty crowded in the perimeter section, let's
00:37 go with the latter. So delete the winded, add the new int
00:43 type And we'll name this one: winded state.
00:50 And we'll leave it initialized to 0. Now we need to fix the transitions.
00:56 From the any state to catching breath, the condition is going to be Winded state
01:04 equals one, and from catching breath to idle, winded state is going to equal
01:11 zero, so we're good to go on this one. In our basic character control script
01:21 let's go ahead and create a matching variable winded state.
01:25 So we'll open that. And I'll go out and grab that from the
01:31 snippets file. Control copy.
01:34 And we'll add it at the bottom here. Control Paste, so here's our new
01:40 variable, winded state, and then next we'll need to coordinate the winded state
01:45 variable with the winded state parameter, since you could easily end up having them
01:50 initialize to two different values... Since the animator uses the parameter,
01:58 lets get that value and assign it to our internal var in the start function.
02:04 So I'll go out and grab that. Control Copy and in the start function,
02:12 Control + V, paste. So this is a little different.
02:19 We've used set integer and set Boole before, but this one is get integer.
02:24 And since it already knows what it's getting, all we need to do is feed it the
02:28 name of the parameter. Now since the trigger into the any state
02:34 is a one shot deal we can put the triggering code into the update rather
02:37 than the fixed update function, and we're going to trigger it with the fire three
02:41 virtual key. But first let's go back out and see
02:46 exactly what that is. So I'm going to go ahead and save this
02:50 script, toggle back to the editor... And from edit, project settings, input,
02:56 we can see that the Fire 3 button, uses left Command, which is fine if you're a
03:02 Mac user, but on Windows, it's going to open the Window Start Menu.
03:10 So I'm going to change mine, since I'm on Windows.
03:14 To right, Ctrl. Back on the script editor, lets go ahead
03:22 and add the code for it. So I'm going to grab that from my
03:27 snippets file, Ctrl C. That's in the update function, so we're
03:34 going to add it. Right here, Ctrl + V, so basically when
03:39 fire 3 equals 1, the players press the button, we're going to call the process
03:45 winded function. We're going to make that thing in a
03:50 minute here. So let's go out to snippets again.
03:54 We're going to grab the whole function, Ctrl + C to copy, and it's a function so
03:59 we'll go ahead and put it at the bottom. We load the on collision enter, Ctrl + V
04:07 to paste. So here's what's happening.
04:13 If the winded state is not 0, in other words, if it's already being processed,
04:16 we're going to return. Otherwise, we go ahead and set the winded
04:19 state to coding errors in the genetic blueprints of some common GM-farmer
04:21 models have led to congenital defects, such as the inability to distinguish
04:23 between terrified migrant workers and large produce items like pumpkins, send
04:25 that message off to the animator, yield, that's one frame.
04:29 Then go ahead and move it to the next value, 2.
04:33 That'll prevent it from looping back to the start of the winded state.
04:37 Then we're going to let the winded state run for a random number of seconds.
04:42 I've used random range, and I'm going between 3 seconds and 5.5.
04:46 And when that's up, we're going to tell the animator again to set the winded
04:51 state to 0 and we're also going to set our winded state variable in the script
04:55 back to 0 as well. Before we save the script, there's one
05:01 more thing we'll want to do. We want to suspend player input because
05:05 if the character is out of breath, he can't very well run off until he catches it.
05:12 To do that, we're going to need a reset input axis.
05:16 So I'm going to go out and grab that line.
05:19 Control copy. And we need to put that at the top of the
05:25 update function. So what's going to happen is, if the
05:31 character is winded Input.ResetInputAxis, basically blocks any of this other input,
05:39 and let's save the script, and head back to the editor.
05:47 And we can test the functionality. So, what we're going to do is from the
05:50 idle, we're going to press the fire 3 key, whichever key that is for you.
05:59 The characters will double over panting to catch their breath and then they are
06:02 going to come up at different times because of the random numbers.
06:06 Let's try it. Click Play.
06:09 Click the fire three, which in my case is the right control, and watch them come up.
06:16 And there they go. If we get them walking forward and press
06:23 the key again, you might be able to catch it just right.
06:27 And some of the characters may miss the message.
06:29 Or rather the message reaches them when they're in the middle of a transition
06:33 marked as atomic. In other words it can't be interrupted.
06:37 So, theoretically we should probably end check atomic on at least the idle and the
06:40 walk transitions. We can do that some other time.
06:44 But for our final test, we're going to have another problem we need to deal with.
06:51 If we try catching the winded state during a turn, we can see another issue.
06:56 The animation may have been overwritten, but the player input to turn.
07:01 Since it doesn't use root, motion keeps going.
07:05 So finally we'll need to set a condition on the code that turns the character.
07:11 Fortunately, we can just check for the winded state value.
07:14 In the fixed update, we're going to need to wrap the whole transform.rotate code
07:19 in the conditional that makes sure that our character is not catching his breath.
07:25 So I'm going to head back to the script, and then go out and grab the last little
07:30 bit of code. From my snippets, and I'm going to go
07:37 ahead and replace the whole rotate line. Control copy, back to the Script Editor
07:47 and in the FixedUpdate will replace the transform.Rotate line.
07:56 Ctrl + V but basically the same thing wrapped in the conditional, so now the
08:01 only time he is going to turn is if the winded state is equal to zero, in other
08:06 words if he is not out of breath. So we can click Save.
08:14 Head back to the Editor. And this time when we click Play, press
08:20 Control, and try to turn. I've got one of my arrows turned, and
08:28 they won't start turning until they come out of the out of breath state.
08:36 So now we've seen that using the any state successfully may require a bit more
08:40 logic than the code to make it work.
08:43
Collapse this transcript
4. Mecanim Masks and Layers
Creating layer masks
00:00 With a lot of the basics under your belt, we're going to star looking at more of
00:03 Mecanim's features. In this video, we'll be invstigating
00:07 masks and layers. Masks allow you to control different
00:10 parts of your character separately. An example would be firing a weapon.
00:15 he could fire from a walk or a strafe or a run, rather than creating three more
00:18 clips with the locomotion and the gun-firing animation, You can just
00:22 override the arm that holds the weapon with the fire weapon clip.
00:28 We'll start by having our character do something a bit more benign.
00:33 If you look at the robot 2 character's animation clips, you can see that he has
00:38 a few head animations and an arm animation.
00:43 We'll use his arm wave clip. R2_wav.
00:45 So the first thing we need to do is make a mask for just the arm.
00:51 Let's select the mech anim assets folder, and then from the right click menu click
00:58 create, and from down near the bottom choose avatar mask.
01:08 And let's name this one Arm Right Mask, and Enter.
01:12 In the inspector, we want to use the top one, Body Mask.
01:20 So let's click to open it, and this brings up the body image where we can see
01:24 the choices. We can make a mask for the root, shown as
01:29 that shadow area down at the bottom, or feet, head, body, arms, hands, legs, and IK.
01:38 Inverse kinnematic goals if you're using Pro.
01:40 Let's start with the right arm. If you click inside the body's boudning
01:44 box area, all parts turn red. They're deselected.
01:49 If you click again, they're toggled back on.
01:54 So let's toggle them off and go 'head and click the right arm.
01:59 And the right hand. That's the characters, by the way.
02:02 That's all there is to it. So let's go ahead and duplicate this mask
02:11 with control D and name this one head mask.
02:21 Head mask, as you're probably going to guess, is just the head.
02:24 And yeah, we're going to have a few of these, so let's go ahead and create a new
02:28 folder inside the Mechanim Assets folder. New Folder, and we'll call this one Masks
02:37 and drag our new masks into it. The robots animations were all prepared
02:44 with the intention of using them with masks.
02:46 Only the body parts that will be included with the masks were animated, but
02:50 sometimes, you may have a full animation and decide that you only want to use
02:53 parts of the body with it for a different purpose.
02:58 To enable you to see the results of masking in specific clips, you can make
03:02 use of the body mask in the animation section where the clip's set up.
03:07 Let's check this out with an animation that's not very subtle.
03:11 Let's use the box man's bog walk clip. And, we're actually going to use better
03:16 box man a. And we're going to select his bog walk.
03:20 And beneath the regular clip stuff, you'll find body mask.
03:28 When we open it up. And we've got that same body mask that we
03:33 saw before. Now I'm going to float my preview window
03:37 and we're going to have a little fun with him.
03:40 So if we click Play, let's deselect the parts one by one, and you can see what's
03:48 happening here. This gives you a way to isolate the parts
03:54 and see what it's going to do with your clip.
03:59 Then you may decide that this guy only needs a few body parts.
04:04 Now there's nothing very useful out of this character by Isolating the parts,
04:09 but you can see how you could use just part of an animation that already
04:14 existed, and be able to create a mask for it.
04:19 This isn't a mask yet, but it just allows you to see what the character's going to
04:23 look like when he does have a mask. That matches it.
04:27 So basically, now I'd create an actual avatar mask with the parts that I wanted.
04:34 In that case, you know, maybe it was the body, the head, and the left arm and hand.
04:40 Anyway, now that we have a couple of masks, we'll be able to use them in the
04:43 animator's layers.
04:45
Collapse this transcript
Layers in the Animation Controller
00:00 Now that we've got a couple of avatar masks, you can jump back into the
00:03 Animator Controller and see about getting them appropriated into the state engine.
00:08 But rather than clutter the Test Controller, let's go ahead and move it
00:11 into the Controllers folder and then duplicate it.
00:15 Ctrl+D and let's name the new one Layer Controller.
00:26 Now, we can start by creating a new layer in the Layers widget and I'm going to
00:30 move my other states off to the side a little.
00:34 And to create one, we're going to click the Plus button, and this one we want to
00:40 name Wave Layer. For the Mask, we're going to choose Arm
00:48 Right Mask and for Blending we want to leave it on Override.
00:57 Note that all of the stuff in the Base layer is gone.
01:00 But don't panic. That's one of the reasons to use layers,
01:03 so you can concentrate on just one specialized section.
01:06 In the upper left, it shows us that we're currently viewing the Wave Layer.
01:11 If we wanted to get back into the Base layer, we'd click on its title bar, and
01:15 we're right back there. Let's click on the Wave Layer's title bar
01:20 and get back into it. We've got the parameters set up, so now,
01:25 we're ready to create the states. The first state is almost always a null
01:30 or empty state. So I'm going to right-click, Create
01:34 State, and I'm going to name this one Null.
01:37 And now we can create another state for our wave, Create State.
01:44 I'll name this guy Wave. And we'll bring our robot's Wave
01:51 animation clip into it. And that's going to be R2_Wave.
02:00 We'll go ahead and create the transitions.
02:06 To control the transitions, we'll need a new Bool type parameter.
02:09 Let's name it Wave Arm. And now we can set Wave Arm, as the state
02:17 to go in, true. And we'll leave the exit time as the exit transition.
02:26 So now, if we put the new Animator Controller on the robot, Layer
02:31 controller, and we'll drop it on Robot2, and select him.
02:37 You can see that it automatically went into place.
02:41 And we click Play and turn the Wave Arm state on, you can see that it's
02:46 transitioning up into it, but down in the View port he's not doing a thing.
02:54 So let's stop and see what we need to do here.
02:59 Turns out that Weight, which is grayed out right now becomes active when we
03:04 click Play, but it also goes down to 0, so the layer weight being 0, we get none
03:09 of the layer. Let's click Play and turn it up to one
03:16 and see what happens. So we click Play.
03:21 We'll turn on Wave Arm again, and now, if we set our weight up to 1, our little
03:27 robot guy is waving frantically. So let's stop Play mode.
03:36 When we hook up the scripting, one of the first things we'll need to take care of
03:39 is the Layer Weight. Meanwhile, let's add a couple more layers.
03:45 Both will use the head mask. So I'm going to click the Plus, scroll it.
03:52 This one is going to be Nod Layer, because he'll be nodding his head and it
03:58 uses the head mask. We're going to leave it on Override, and
04:06 the next one, we're going to name Talk Layer.
04:11 It also uses the head mask. And this one, instead of Override, we're
04:17 going to set to Additive. This way, our character will be able to
04:24 talk at the same time he's nodding. So I'm going to go back to my Wave Layer,
04:32 and I'm going to copy the null state and I'm going to give each of the other two
04:40 layers, a null state by pasting, and then I'll go ahead and add their states.
04:55 This one is Talk. And I'll add the robot's talk animation, R2_Nod.
05:07 Quickly put in my transitions while I'm here.
05:15 Then we'll head up to Nod. Finish him up.
05:20 Right click, Create State. This one's going to be Nod.
05:25 And it gets the robot's Talk state. And I'll set up its transitions.
05:42 So now, the only thing we need now are two more parameters, one of Nod, one for
05:46 Talk, but we're getting pretty crowded here.
05:51 So what I'm going to need to is I'm going to need to go up and fold up my layers.
05:57 So I'll make sure I'm getting my Plus from my parameters instead of the layers.
06:05 So the first one is a Bool type and that's going to be Nod, and the second
06:10 one is also a Bool type and it's going to be Talk.
06:17 Now that we've got our parameters, we can go ahead and hook up the transitions.
06:25 So in Nod, the condition is Nod equals true, and we'll make the exit condition N
06:34 equals false. Then I need to select my Talk layer.
06:43 Its transition in will be, Talk equals true, transition out will be Talk equals false.
06:54 So now, the next thing we're going to need to do is take care of the scripting.
07:00 But if you want to go ahead and test the new layers now, don't forget you're
07:03 going to need to set the weighting to 1 before you'll see the results.
07:07
Collapse this transcript
Activating the layers
00:00 With the masks and layers set up, our next step is to trigger the layers from
00:04 the script. Let's start by opening the base character
00:08 control script. Remember the weight issue we discovered
00:11 in the layers? To take care of that, we need to find out
00:14 how many layers we have Then set their weights to one or 100%.
00:20 Since we'll need it in a couple of places, lets add a variable to store the
00:23 number of layers. I'm going to go out to my Snippets file
00:29 and copy it, Ctrl+C, and put that variable up with the others.
00:38 Ctrl+V, and then once we've identified the animator in the start function, we
00:44 can check to see how many layers it has. So I'll go out and get that line.
00:51 Control copy. And I'm going to put that underneath the
00:57 animator line, because at that point, we can use animator.layerCount and assign
01:04 the number of layers directly to our new variable.
01:11 Now we can iterate through them and turn the layer weights up to 1.
01:18 I'll go out and get that code. Copy.
01:19 And I'm going to put it right underneath here.
01:31 Paste, Ctrl+V. Indent.
01:34 And if you look, we're only going to iterate through assigned numbers if
01:41 there's more than one layer to begin with.
01:48 Now, this is a pretty handy piece of code to have, so I'm going to go ahead and
01:53 save this script, close it out in the script editor, and go back to the regular
01:58 editor, and use Ctrl-D to make a copy of it.
02:04 This copy, I'm going to name Layer Control, and I'll go ahead and drag it
02:11 onto our robot character. Now I've still got the other script on
02:19 it, so I'm going to need to remove it. Right click > Remove component.
02:28 To trigger the new animations we're going to use fire one and fire two inputs for
02:32 nod and talk. Fire one is the left mouse button and
02:36 fire two is the right mouse button and they've also got key board equivalents.
02:43 So let's go ahead and get the Fire2 code in first.
02:46 I'm going to open the new Layer Control Script, and then go out to my Snippets
02:51 file for the code for Fire1, the talk animation.
02:57 Copy, and we're going to add this in the update with the rest of our input code.
03:10 Ctrl+V to paste, indent, let's see what's happening here.
03:19 If the player presses Fire2, we're going to set the Talk parameter to, well, the
03:26 negative of what it currently is. So we're going to get it first and use a
03:34 negative of it. To reset it, so basically we're toggling
03:38 this guy off and on every time we press the button.
03:41 For the fire 1, the nod will let the exit time turn it off, but we still need to
03:46 turn the perimater to false so it won't keep triggering.
03:51 Let's go and get that code. Control copy, make some room for it,
04:01 Ctrl+V to paste. And we'll indent it.
04:11 So this time, when the player presses the fire 1 button, we're going to set nod to true.
04:19 This is going to start the animation. This will, this will initialize the state.
04:24 But immediately after that, the next time this comes through the code, we're going
04:28 to set it to false. It doesn't matter because the animation
04:32 is going to play regardless. Okay, so now we can save the script and test.
04:37 So we click save, go back out to the editor, and we've already put our new
04:41 script on our robot character its right here waiting for us he's got his new
04:46 layer controller up here so we should be good to go.
04:52 I want to go ahead and switch to the scene view so we can zoom in on him and
04:56 get a better view. And we'll turn the light back on.
05:01 So we'll click Play, and when I click my left button, my Fire 1, he opens his
05:08 mouth and shuts it, and that's not what he should be doing.
05:16 I'm pretty sure he should be nodding. If I click my right button he does talk,
05:21 so that one's okay, and if I click it again he stops.
05:25 So it looks like I need to head over and check to make sure that I've got the
05:28 right clips assigned to the layers. So I'm going to go over to my animator
05:34 and see if I can spot the problem. In my nod layer, I'm going to select nod,
05:39 and sure enough, it was set to talk. So I'm going to open that, and put nod in
05:45 like it should be. As long as I'm here, I'm going to check
05:48 both of my transitions. Nod equals true, he goes into it, and nod
05:53 equals false, he goes out. Well that's a problem right there,
05:58 because of my code. So I need to change this one back to exit time.
06:06 Now, if I go to my scene window, click Play, and left click, he nods and stops
06:11 when he's finished nodding. If I right click, he starts talking.
06:19 And if I click nod, he continues to talk while he's nodding.
06:25 So everything's working well now. Click Stop and, because the Talk layer is
06:31 additive, our character was also able to nod at the same time.
06:39 So Layers can override to add to an existing animation, but the important
06:43 thing to remember is that we need to set the weight to 1 in the script, to see the
06:47 layers in action.
06:50
Collapse this transcript
Handling nonhumanoid appendages
00:00 Now that you've had a taste of Masking and Layers, it's time to find out how to
00:04 handle those body parts that don't fall under the normal humanoid bone system.
00:09 It turns out your humanoid can have them, but they won't be included in retargeting.
00:15 They also need to be handled differently for Layers and Masks.
00:19 Let's start by loading the base set up scene and deleting the characters.
00:23 So, we're going to switch over to our base set up scene and I'm going to delete
00:27 the characters that are already in it. And then I'm going to rename this one
00:34 Xcharacters as an Xman. And I forgot to save it into my Scenes
00:40 folder, so I'll just go ahead and drag it in there now.
00:45 Next, we'll need a test subject, so when we do import the Mrs Bot.unitypackage
00:51 from this Videos Exercise Assets folder. I'm going to right-click, Import Package
00:58 > Custom Package. I'm going to go out to my Exercise Assets
01:04 folder, and I'm going to bring in Mrs. Bot, and Import.
01:12 She comes in with a couple of motion clips defined, and a prefab with a
01:16 script, simple Input Control, and a simple Animator Controller, Base
01:20 Controller, already in place. So you're probably wondering what she
01:25 looks like, so let's go ahead and select her from the Prefabs folder and then drag
01:29 her into the scene. She's pretty much what you'd expect along
01:34 with the addition of a large sun hat with a rather unattractive flower.
01:37 Let's select the Mrs Bot Asset from the ThreeCharacters folder and check out her animations.
01:44 I'm going to right click to float the preview window and then we'll look at her clips.
01:51 Your idle clip looks like it could be something out of a 1950's Sci Fi flick.
02:00 For the other clip, Audrey, we're going to need to tip the view a little so you
02:04 can see what happens. And I click Play and her flower gets a
02:09 little bit aggressive. Must be why she's named Audrey.
02:15 Okay, let's see about being able to call up the Audrey animation as a layer.
02:20 I'll go ahead and close my preview. And go over to my Animator window.
02:26 And double-click on Mrs Bot. So, right now we have the Base Controller
02:32 on her. And I want to go ahead and set one up
02:35 just for her, so I'm going to select the Base Controller and Ctrl+D to duplicate
02:41 it and I'm going to name this one Mrs. Bot controller.
02:47 And I'll go ahead and put that one on her.
02:49 And of course, it looks the same because we just duplicated it., In the idle we
02:56 need to load Mrs. Bots idle animation, Mrs.
03:00 Bot, Idle. And then for Forward, she doesn't have a
03:07 Walk animation so we'll just go ahead and put in the normal Better Box Man Walk.
03:16 And we'll check our transitions and they're already set up for the V Input
03:24 parameter that was already here. If we click Play and test, we'll see that
03:33 the flower animates at the Idles by the clip, but not the walk since it was from
03:36 a different character. So there she is in her idle, and I'll
03:44 walk her forward with my W key. She's walking out of the scene so I think
03:50 what I need to do is I need to stop And let's reposition our main camera so we
03:56 can actually see what she's doing. Game object, align with view, and that's
04:05 much better. So now if I click play we can definitely
04:13 see her idle animation... And if I click the forward arrow, you can
04:19 see that the flower's no longer animating.
04:24 So let's go ahead and stop playback. So the question is, how can we trigger
04:28 the flower in a layer? Because it's an extra humanoid feature,
04:32 there's no mask features to set up. Let's start by creating a layer and
04:38 naming it flower idle layer. For the flower idle layer, we always want
04:43 this one to be on. We want it to override whether she's in
04:47 the idle or whether she's walking. So I'm going to right-click and instead
04:52 of creating the usual null state I'm going to make this one flower idle, and
04:58 I'll go ahead and load her idle animation into it.
05:05 Since this one should always be on it doesn't even need any transitions.
05:09 Our script has already set the weights, so if we click Play and try to drive Mrs.
05:14 Bot forward... We're going to see that the flower idle
05:16 layer now overrights the walk and she doesn't move at all.
05:20 Let's test it and see. So she's idling and if I press my forward
05:25 key she doesn't do anything. And that's just as we would expect,
05:31 because the idle layer is overriding everything.
05:36 Now let's make our second layer. We'll name this one flower attack.
05:44 For flower attack, we do need a null state.
05:48 And we'll need the flower attack state. And for the motion, that will be the
06:06 MB_Audrey clip. Before we can do the transform, We'll
06:13 need one more parameter. So I'll add a bool and we'll name it,
06:19 guess what, Flower Attack. Now we can set up our transition so that
06:28 it goes in when Flower Attack is true. And goes out when flower attack is false.
06:39 Let's make sure that Mrs. Bot is selected in the hierarchy view and
06:46 see how it's working. So selector So now what we would expect
06:53 is the flower attack layer is below the flower idle layer so right now we're
06:58 going to expect it to override the first one when we check our flower attack parameter.
07:08 So I'm going to click play. And if I click the flower attack The
07:13 entire flower attack overrides the original idle, so her body isn't moving
07:18 this time. Let's stop Playback and think about what
07:23 we've got. The rest of Mrs.
07:26 Bot didn't animate because there was no animation on the rest of her during the
07:31 Audrey clip. What's more, since the attack layer's
07:36 below the idle layer, it overwrites the idle layer.
07:41 So now what we need is a mask just for the flower.
07:45 We know we're going to need a mask but we have discovered that the order of a layer
07:50 is important. The last layer in is going to overwrite
07:55 the ones above it. At least if it's using the same body or
07:59 body parts.
08:01
Collapse this transcript
Creating masks for nonhumanoid appendages
00:00 In the last video we set up the layers for the attack flower.
00:03 But now we need to solve the mask problem.
00:06 Fortunately, Mecanim provides a solution for us.
00:09 So let's go ahead and create an avatar mask and name it Flower Only > Right
00:15 Click > Create > Avatar Mask. And we'll name it Flower Only.
00:24 This is where we need to use the Transform mask.
00:27 But first, we have to clear the Body mask since Mrs Bot is humanoid.
00:33 So let's close this, and then open the Transform mask.
00:37 And the first thing we need to do is load her skeleton.
00:42 So we're going to click Assets, and choose the Mrs Bot Avatar.
00:47 Then we need to import the skeleton. And you can see it comes with her entire hierarchy.
00:56 Fortunately we can turn it off quickly with a few clicks, and there's a few down
01:03 here we need as well. And now we need just the bones that go
01:06 with the flower, and that starts at bone 01, and because it's a hierarchy clicking
01:12 the first one gets the rest of them for us.
01:15 So now we're ready to go ahead and set up our layers with our mask.
01:19 I'm going to open my layers, and I'll load, the new Flower Only mask, into each one.
01:32 And we should be ready to test it. So now when we click Play and walk
01:36 forward, the flower will play its part of the, idle animation.
01:41 Let's click Play and give it a try. So the flower's animating.
01:46 If I press my Forward key, the flower still animates, and she's using the walk
01:52 cycle from a different character. Let's stop play mode.
01:57 And now let's try our (INAUDIBLE) clip, our flower attack.
02:01 To do that, we need to make sure we select Mrs Bot, then we can click Play
02:06 and in the Idle, when we click Flower Attack, the flower immediately goes into
02:12 a snapping animation. If we turn it off, it stops.
02:17 Turn it on, and this time, let's move her froward, and it also continues to play
02:24 even though the walk cycle belongs to a different character.
02:27 So let's stop play mode, and do a recap. So now we've seen that humanoids can have
02:33 X-Men-like features, and I mean things like wings, and tentacles, and antenna.
02:40 And other things that aren't normally part of a humanoid bone system.
02:44 If they're only associated with regular animation clips, we don't really need to
02:48 do anything differently. But if they have animations that need to
02:51 be activated as layers, we can created custom layer masks to control them.
02:56
Collapse this transcript
5. Mecanim State Management
Creating substate machines
00:00 With all the current functionality in our test controller, our animator view has
00:04 been getting pretty complex. So in this video, we'll see about tidying
00:08 it up a bit, as we look into a few more state and transition options.
00:13 I've opened up the character set-up scene, and I've selected the test controller.
00:18 And yeah, it's starting to look like a spaghetti factory.
00:21 So we're going to start with substate machines.
00:24 These give us a way to compartmentalize sequences of states.
00:28 A perfect example would be the race sequence, where the character goes from
00:33 on your mark, to get set, to go. So, let's create a sub-state to store the
00:40 racing start up states in. We need an empty spot near Idle, but
00:45 close to the Run, so let's make some room.
00:49 And I'm going to right-click in the empty spot, and instead of create state, we're
00:56 going to create sub-state machine. And that's what this guy looks like.
01:01 It comes in as an elongated hexagon, and it's blue.
01:06 And we'll name it Start race, and yeah, it's pretty vacant over there in the inspector.
01:13 If we double click the new sub state machine, we're taken to its private area
01:17 in the view where you will see its new default state, up.
01:21 And I'm going to drag it over so we can see it better.
01:24 And you can see where we are in the animator hierarchy, by checking the top
01:30 bar again. We're in Start race, and there's Base layer.
01:34 You can get back to base layer either by clicking on the name there, or you can
01:41 double click on the new place holder, Base layer, and it takes us back into the
01:46 base layer. So let's head back to the start race and
01:50 add a race related states. We're going to need one for on your mark,
01:54 one for get set, and one for go. So I'll Right click, Create Empty, Right
02:00 click, Create Empty, and we'll need one more.
02:08 And there we go. This one is on your mark.
02:17 This one is get set. And this one, is go.
02:21 For the animations, we're going to add BB on your mark, BB get set, and BB run.
02:47 Now before we start doing our transitions you're probably going to figure out that
02:52 if you right click on the base layer we only get the copy and paste options, so
02:57 let's go ahead and set up the rest of our transitions next.
03:03 Make transition, make transition. I'll go ahead and set these up first.
03:11 We can do this using the race state parameter we created in an earlier video.
03:15 So the transition into get set is race state two.
03:21 The transition into go. Is raise state three.
03:28 Now let's go back to the base layer and set up the transition into the start race
03:33 sub machine. So I'm going to double click on my up
03:37 layer, and we want to go from idol to on your mark when the race state equals one.
03:44 So I'm going to make a transition. And we want it to go to on your mark, and
03:50 then we select it and that's going to happen when race state equals one.
03:59 If we try to make a transition back we're going to get the same copy and delete, so
04:05 let's Double Click on our race state again...
04:10 And now our go needs to go back to the regular base layer.
04:16 So I'm going to right click, make trasition.
04:19 And it's going to have two options. We're either going to go to the idle or
04:26 the run. Let's set up the run first.
04:29 For the run condition to be true, we need to use exit time.
04:35 We're going to let the player have that exit time of that run clip to be able to
04:40 get himself set and ready to hit run. So the first thing is exit time.
04:47 The second condition is, if the player is set and ready to go, he's going to have
04:52 pressed the Forward key, so we're also going to be looking for V input greater
05:00 than .1. If he doesn't do that, if he's forgotten
05:03 to press his Forward key, we're going to make a transition back into Idle, and
05:08 that's going to have a similar transition In that we're going to wait for our exit
05:16 time before we do anything, then we're also going to stipulate that the input is
05:22 less than .1, so in other words, if the player forgot to press the forward key,
05:30 this guy's going to be left standing at the gate.
05:35 So now we ought to check it out to see if it works.
05:38 In case you've left the raised state up to something other than zero, let's make
05:43 sure that we are at zero. And I am, so I'm good.
05:46 And then we'll select one of the characters.
05:50 I'm going to go for Lenny this time and click play.
05:57 Right now he's in idle. As soon as I set race state to one, he
06:03 drops into on your mark. When I set it to two he goes in to get
06:09 set, when I type in three he's off and running but since I didn't press my
06:17 forward key he's gone back to an idol so he's off standing somewhere here looking
06:23 around; let's stop playback So, let's have a little fun with this.
06:28 Let's go ahead and speed up the run clip in the Go state to 2.
06:36 So, he's going to be really fast off his mark.
06:39 And, now this is when we need to be able to be interrupted.
06:42 So, let's uncheck atomic on both of these exits.
06:47 So in the run, go to run, we'll uncheck atomic.
06:52 And in the go to idle, we'll also uncheck atomic, in case the player is ready to
06:58 get going. So as soon as he's in state 3, we should
07:01 be able to get him running. But I haven't figured out how to type in
07:05 three and click fast enough to switch the focus to the game window, but it should work.
07:11 So now we've got a pretty good portion of the race functionality start sand we've
07:15 managed to do so without adding much more clutter, and by now you're probably
07:20 getting an idea of how complex the logic behind transitions can get.
07:25
Collapse this transcript
Configuring Blend Trees
00:00 In this video, we'll be tackling blend trees.
00:03 Mecanim's solution to blending between similar states, without turning the
00:07 animator view into a snarled mess. The classic blend tree is the blend
00:12 between a turn left run, a straight run, and a turn right run.
00:17 The input value for the virtual turn key goes between minus one and plus one,
00:22 controlling which animation is being played.
00:27 Let’s get started by creating our own blend tree.
00:30 We can right-click, and from Create State, choose From New Blend Tree, or we
00:37 can choose an existing state and turn it into a blend tree.
00:46 Let's do that with our run state. Select it, right click, and now create
00:51 new blend tree and state. And it doesn't look like anything's changed.
00:58 But if we double click on it. We see it in its own little area in the
01:03 animator window. As with the sub-state machine you can see
01:06 where you are in the hierarchy up in the left hand corner, so we're in the blend
01:10 tree, and you can get back to the base layer that way as well.
01:15 The blend tree handles loading of motion clips completely differently from the
01:21 rest of the states. In the Inspector we need to add the three
01:26 new Motion Fields, so we go over here, we click the Plus, Add Motion Field, and
01:31 note you could also add blend trees so you can nest them, but we're just going
01:35 to add a regular motion field and we're going to need two more.
01:44 So I'm going to go ahead and add blend, and you can see now that set up to blend
01:50 between the three motion fields and next we're going to go ahead and load Run
01:55 Right, Run and Run Right again. So let's choose Run Right.
02:05 In the second one, we'll choose run, and in the third one, we're going to choose
02:13 run right again, and now we can see them set up.
02:21 With our blend tree. If you get them in the wrong order, you
02:26 can select them here and move them around.
02:30 In our second run right, we're going to mirror it.
02:36 So I'm going to click the Mirror button. So let's see what we have now.
02:40 The perimeter V input was inherited from our re-purposed run state.
02:46 Let's look at our other options. If we click the down arrows, we'll see
02:52 that we only have one other option, Turning.
02:57 And if we check through our list of parameters over in the Animator window,
03:01 you'll notice that Turning and V-Input are the only Float-type parameters.
03:09 Considering it will be blending the clips between a normalized value of zero and
03:13 one, it's not surprising that we are going to need Floats.
03:17 So let's create a new Float type parameter and name it Run Type.
03:23 Is expected it turns up in the drop down. We won't be needing it until later, but
03:28 it's worthwhile knowing that we can only run blue trees off of float parameters.
03:34 Let's go ahead and select Turning instead, to the right of the clips the
03:38 next column is threshold. Right now its grayed out.
03:43 And has a default over the normalized zero to one values.
03:46 Because of the Automate Threshold option. The next column is speed.
03:52 And I'll tell you right now, the run Right Clip is way too fast.
03:57 So lets go ahead and set the speed on those two to .5 for starters.
04:02 So this is where you adjust the speed of clips in a blend tree.
04:07 The last column lets you mirror the clip. We've used the Run Right clip twice, so
04:12 we went ahead and mirrored one of them, to turn it into a Run Left.
04:17 There's lots more to look at but we'll finish that out in the rest of the chapter.
04:22
Collapse this transcript
Exploring Blend Tree transitions
00:00 We started setting up our Blend Tree, but we haven't really looked at the
00:03 transitions yet. So, now's a good time to see how they work.
00:08 Let's go ahead and test the Blend Tree now.
00:10 First, I'm going to go ahead and float the Preview window with a right click,
00:15 and then in the Animator View let's drag the blend trees slider slowly back and forth.
00:22 The clips are blended according to the threshold values as you can see by the
00:26 highlight on the clips in the animator window, and also the location of the time
00:30 indicator in the blend graph, this red part here.
00:34 Let's try that again. And you can see it moving as well.
00:38 The top of the peaks are 100% influence. And you can also drag the indicator in
00:47 this area, for the same results. But be careful not to move the peaks by mistake.
00:57 So let's click Play in the Preview window and scrub slowly back and forth.
01:02 The first thing we'll notice is that the turns are still running way too fast, so
01:07 I'm going to click Play, and then I'm going to scrub between three animations.
01:14 So let's go ahead and set those to a speed of about .4 each.
01:22 And the run will probably match better at about 1.2 a lot of times, Blend Trees are
01:28 used for animations that have similar speeds.
01:35 So sometimes you'll want to try Adjust Timescale, and make it homogeneous speed.
01:40 We'll try that. And you can see the numbers it's changed
01:44 up here. Then, if we click Play, it doesn't really
01:48 do too much for us. So, I'm going to stop and set it back to
01:58 my original values 0.4, 1.2 and 0.4. But if you had clips that were similar in
02:05 speed that probably would be a pretty good thing to try.
02:09 Now, the next thing we need to look at is the threshold.
02:12 Since our turning goes between minus one and one, we need to uncheck automate thresholds.
02:19 And put in our own numbers. So we want it to go from minus one, to
02:25 zero, to positive one, because we're going to be using it with the turning key.
02:34 Now there's lots of presets under the compute thresholds that will do
02:37 calculations for us, but for this Blend Tree we're just going to use input values
02:41 so it's pretty straightforward. Now we need to check on one more little issue.
02:47 When we scrub back and forth the character seems to hop at the transition point.
02:54 So I'm going to click play again. And watch when he goes through and see
02:59 him hopping there. So when our transition point, and that's
03:06 a bad thing. So we need to go out and figure out what
03:10 happened with him. If we select Better Box Man A in the
03:14 Project Field and check on this Run clip. We can see that he starts out on the
03:24 right foot, and if we drag the slider he has got two strides to four strides.
03:35 And then if we check on Better Box Man C, where Run Right comes from We can see
03:42 that it also starts on the right foot. And it also has two full strides.
03:52 For Blend Trees to work correctly, all of the clips need to start on the same
03:57 front, and have the same number of cycles, since it uses percentages of the
04:02 clips for the blend. So what happened?
04:08 Well, we mirrored the run right clip, so the leading leg changeds.
04:14 If we go back to our Blend Tree and scrub through again, we can clearly see that he
04:19 switches legs right here. Fortunately because both of the clips had
04:25 two cycles each, I was able to switch the start and end times to get one full
04:30 stride with the left leg leading, and the other with the right leg leading.
04:37 Then I mirrored the left turn version in the clip.
04:41 And to match those two, I also needed to generate a one stripe from the straight run.
04:45 So now we can go ahead and run the BB run right one, BB run one.
04:52 And BB run left one, and now when we scrub the turning slider and leading leg
04:58 stays the same the entire length, and now we can go ahead and click Play in the
05:04 preview and test the midpoint transition. And finally, the midpoint transitions are smooth.
05:17 So it looks good in the Animator, but next we'll need to put it through a
05:20 stress test in the actual scene.
05:23
Collapse this transcript
Stress testing the run blend
00:00 The next thing we need to do is test our blend tree in the actual scene.
00:04 To make sure he's running I want to put my Race State to 3 and I'm going to click
00:08 Play and try running and turning him. And you can see that the turns are a
00:14 little bit fast. And that's because we're seeing the extra
00:19 speed because the script and the clip's angular velocity are being angled together.
00:26 To keep the blend trees run turns from receiving extra turns from the script, we
00:30 could easily add a check for the run blend state before adding the turn from
00:34 the scripted rotation. But we've got a bigger problem using a
00:39 jump from the blend tree. We're going to need a dynamic jump to see
00:43 what goes wrong and that will be an addition we need later on and it's quick
00:47 to set up. So lets just do that right now.
00:50 We're going to create a state over by the blend tree back in the Base layer.
00:58 In fact while we're there let's go ahead and rename run to Blend run.
01:07 And then we'll add a jump right here. Right click Empty.
01:12 Name this guy Dynamic Jump. And the transition from Blend Run.
01:26 And I should probably turn off my Play here.
01:28 The transition from Blend Run to Dynamic Jump is going to be Jump equals True and
01:36 the transition back is Jump equals False. For our jump clip, we're going to use a
01:48 clip called Long Jump. This one contains forward velocity.
01:56 So let's go ahead and look at the transition and see what it looks like.
02:03 I'll float my Preview. I'll click Play.
02:08 And there it is. And you can see he's suspended in midair
02:10 for quite a while. If we want to shorten the amount of that
02:14 jump in the exit transition, we could move the whole transition further to the left.
02:21 And that would give him shorter jump time but I'm going to leave it as is.
02:27 So now in our basic character controller script.
02:31 We're going to need to add an else clause to the add force conditional.
02:35 And that's going to trigger the Dynamic Jump.
02:39 I'll go out to my snippets file for that and grab it.
02:45 Copy. And in the fixed update, right under the
02:50 if stateInfo equals Base layer Idle We're going to add our else.
02:59 So in other words, if he's not in Idle we have a few other conditions here.
03:09 So first off he's not in the Idle state if Race State equals 3, which means he's running.
03:16 And he's not turning and that's going to be important.
03:20 Then we're going to do the regular jump checks.
03:24 Then we're gona give him a bit more force since he's running.
03:28 So I've set that one to 500. And then we go ahead and process the jump
03:32 like usual. While we're at it, let's go ahead and add
03:35 a grounded condition to the rotation's conditional.
03:40 And that's right here. So right now as long as he's not in the
03:44 winded state he can rotate but we're also going to say he can't rotate when he's in
03:49 the air. In other words he can only turn if he's
03:57 not winded and he's grounded. So I'll add And grounded.
04:06 So grounded has to evaluate is true. Okay now we can save our script and test
04:11 the new code. Save, head back to our scene and I'm
04:18 going to go ahead and go over to my scene view here.
04:27 So we'll have a little better view of them.
04:32 And now when we click Play and turn and jump, you can see he's still turning.
04:41 So we've got one more little thing to solve here.
04:47 Let's stop Play mode. The problem is that we've stopped the
04:50 script from turning the character but we still have those animation clips that
04:55 have angular velocity and they're what's turning them right now.
05:02 So in this case we're better off to use a version of the turn that doesn't turn the character.
05:09 We can still use a lean on the character but this time we're going to let the
05:12 script do all of the turning. So I'm going to go back into the
05:20 Animator, select my Blend Run and I'm going to change these out for Run Lean
05:33 Right One and Run Left One. And these guys also aren't as fast as the
05:46 original turns so lets their speeds to maybe 0.6.
05:51 And now we can go ahead and test the Run Turn Jump sequence again.
05:56 So I'm going to switch over to my Scene view here, click Play > Run > Turn and Jump.
06:02 And you can see now when they're in the air, they continue to go straight.
06:15 So the jump force and transitions could stand a little tweaking but we've got our
06:20 blend tree working nicely. So our Run Blend Tree might have started
06:26 out as a less than perfect solution but once again a little logic and a bit more
06:30 code made it a fine addition to our animated controller.
06:34
Collapse this transcript
6. Alternative Motion Control
Controlling states with speed
00:00 While using input key values as controllers is probably the most common
00:03 means of managing a character's locomotion states, it's by no means the
00:07 only method. In this video, we'll take a first look at
00:12 using speed to control the state transitions.
00:15 Let's start by loading a new scene. Speed test, unity package.
00:20 So I'm going to right click, Import > Custom Package.
00:30 Go out to my Exercise Assets folder, and select Speed Test, and Import.
00:38 So now, we've got a new scene called Speed Test, and let's go ahead and
00:41 double-click again. We don't need to save this one.
00:44 And it looks pretty much the same. But if we check the characters, we'll see
00:50 that instead of the basic character controller, we've got the accelerator
00:55 script and we are also using a speed control for our animator controller.
01:03 Lets take a quick look at that. So we're going to start out looking at
01:10 regular transitions here. Just a simple transition from idle to
01:13 walk to run eh, o a faster run. But before we hook up the animator.
01:21 Let's go ahead and see what the movement our script is giving it before we hook
01:24 them together. I'll click play, and we're going to have
01:28 to click to start the action. And you can see, it starts slow And it
01:34 gets faster and faster. And then it's going to stop when it hits
01:38 a certain speed threshold. So now we need to go ahead and send the
01:43 speed value from the script back to the animator controller.
01:49 So I'm going to open up the accelerator script.
01:53 Head over to my snippets, and grab the line I want.
01:59 Ctrl+copy An we need to add this one, in the fixed update, after the speed has
02:05 been, updated. An I'm going to Ctrl+V to paste.
02:15 So let's look an see what this thing is actually doing.
02:17 So we've got a variable name moving, an that's what tells it to go ahead an move.
02:23 So as soon as we press the left mouse button, the fire one, moving value is set
02:27 to true. When it's true, each time it goes through
02:32 the fixed update, our speed is going to increase by 0.03.
02:37 And, just like we were rotating the characters, this time we're going to
02:41 translate them or move them using the speed we've set up there, time dot delta
02:46 time, which is what breaks it into a particular time per second, And it's
02:50 moving in the z or forward direction. Now if the speed, which we've been
02:58 incrementing here is greater than ten then we're going to set moving to false,
03:02 and that's what stops them, and when all that has been calculated now we're going
03:06 to tell our animator controller what the current value of speed is.
03:12 So let's go ahead and save... Head back to the editor, and we'll go
03:18 ahead and click Play again. And this time, watch the speed relative
03:23 to each other. Before, remember, they went off at the
03:27 same speed, and kept going at the same speed?
03:31 Now, if I click Play, you can see that the longer legged characters quickly
03:35 Outstrip the shorter leg ones, and they even go off the ground.
03:41 So let's stop and figure out what happened.
03:45 There's actually two things going on here.
03:48 The first is that in the animator controller, remember that parameter
03:52 called "apply route motion"? Let's take a look at it.
03:57 This guy right here, that is what tells mechanim to move the character at the
04:01 velocity of it's animation clip, and right now, that is being added to
04:05 whatever speed our script gives it. Now we could uncheck Apply Root Motion
04:12 for all of the characters. And I'll go ahead and do it, because it's
04:16 pretty quick. So we're going to uncheck Apply Root Motion.
04:24 And if we click Play again, we can review part You can see that now they're not
04:30 getting the extra speed, but it's pretty rough looking, isn't it?
04:37 So we're going to do a couple of things, and the first is, rather then unchecking
04:41 Apply Route Motion for every character you put in the scene, when they're going
04:45 to be controlled by the script. There's something else we can do.
04:51 Let's open up our accelorator script again, and we're going to change
04:57 FixedUpdate to OnAnimatorMove. And then we'll save.
05:05 So it's basically going to behave the same as the fixed update, but it does a
05:09 couple of other things for us, and I'll get back to my editor, and the first
05:12 thing you'll notice is if you'll select a character, now, instead of a check box
05:17 for apply root motion. That tells us that it's being handled by
05:24 the script, and that's kind of a nice reminder, so if nothing else, it's worth
05:28 using for that, and if we hit play again and click, you can see that it does the
05:32 exact same thing as to apply root motion off.
05:40 So now lets see about doing something with those transitions because they are
05:43 pretty rough. With conventional input access values,
05:47 when the player is controlling the character, we were using one and minus one.
05:52 And we just had to live with it. But since we're controlling the
05:56 animations according to speed, we've got an ideal scenario for using speed in a
06:00 blend tree, and it just so happens I've got one ready for you, so the first thing
06:05 we'll need to do is right click on it and set it as the default, and let's go ahead
06:09 and click on it and see what's in it. So what you can see is I've added an idle
06:20 a walk and my two runs I've also increased the speed of that second run.
06:30 So this blend tree, instead of blending between clips that are at the same speed,
06:34 is doing just the opposite. It's blending as the speed increases.
06:40 Now, you'll also notice that the transitions are quite a bit different in
06:45 this one. And this is a perfect time to use compute thresholds.
06:51 And tell it to compute with speed. Now the only thing it didn't do on this
06:57 one, is it didn't double time it. So, I'm going to go ahead and set that
07:01 one to 11. But what it's done is it's gone out and
07:04 it's looked for the velocity of the speeds on its own and matched it as the
07:09 thresholds, so that's a pretty handy thing to have.
07:14 So now let's go ahead and click Play again.
07:16 Pick in the view port And look at how smoothly the transitions are this time.
07:23 Now, we can do something with the stop, but, it's going to be abrupt anyway
07:28 because of the scripting. So now you can see the beauty of a speed
07:33 control blend tree. The characters blend smoothly between the
07:37 various animation clips. So now we've seen how to use speed to
07:41 control blend trees, as well as using on animator move To block root motion from
07:46 the animation clips.
07:50
Collapse this transcript
Using Mecanim with PathFinding (Unity Pro only)
00:00 So we've had a taste of controlling a character's state without player input,
00:04 but it may have seemed kind of useless unless you're thinking of AI and pathfinding.
00:11 So this time we'll be doing a simple test using Unity Pro's pathfinding system to
00:15 control the character's animation, while we let Mecanim handle the transitions and states.
00:23 This little scene takes a bit of time to set up, so I've prepared a Unity package
00:26 for you to use with a good portion of the scene set up and waiting for you.
00:31 In this little scenario, the character has orders to go to a target location.
00:36 If you have Unity Pro, let's go ahead and import the package, MecanimPathFinding,
00:41 from this video's Exercise Assets folder. So we're going to right-click, Import
00:50 Package > Custom Package, Exercise Assets, and MecanimPathFinding, and Import.
01:03 This one contains a scene called Nav Test.
01:06 Let's go ahead and load that, and we don't need to save.
01:12 Let's get a better view and see what we've got.
01:14 So it consists of a few walls, a ramp, platform, and a goal.
01:21 All but the lights, camera, and sphere are marked as Static over here, and
01:27 especially as Navigation Static. So the first thing we're going to need to
01:33 do is start by baking the Navmesh. If you have Unity Pro, you can open the
01:37 Navigation panel from the bottom of the Window menu.
01:41 With this package, it's come in already baked, so we're good to go on that.
01:47 We've also got a character waiting for us, and he's just outside the platform,
01:51 opposite the ramp side. But right now, if we check the inspector,
01:57 the only thing he has is a script and his animator component.
02:01 To get him to work with pathfinding, we're going to need to add Component >
02:05 Navigation > Nav Mesh Agent, and then we'll change a few settings in here.
02:14 The speed we want to match the speed of our run, which was about 5.33.
02:21 And I found his angular speed was pretty laggy, so I'm going to bump this up to 500.
02:27 For the stopping distance, since the sphere with the radius of 0.5 is the
02:30 goal, let's go ahead and set the stopping distance to 1, so he'll stop before he
02:34 runs into it. I've already added a script to control him.
02:41 It's called Mec Agent. We should go over there and check it out quickly.
02:45 We've got a few things going on here, but not much.
02:50 We're identifying our animator controller, we're identifying our agent,
02:55 that's our guy, that's our character, and we're also giving him a destination.
03:03 And it's target, and we're going to have to set that back in the editor.
03:08 So we can close this out. And under Target, let's go ahead and drag
03:14 the sphere in as that parameter. So now if we click Play, we can see that
03:22 the path finding is working. He heads off, navigates around the
03:30 obstacles, and finds his target. So let's stop Play mode.
03:39 So now let's go ahead and get Animator to control the clips.
03:43 I'm going to start by selecting the Speed Controller that we created in an earlier
03:48 video, and dropping it on him. And so now that's our controller, and
03:54 we're also going to need to make a few changes in our Mec Agent script.
04:01 So let's open that again, and I'm going to head out to the Snippets file.
04:08 And grab our new function, the OnAnimatorMove function.
04:14 Ctrl+Copy, and we'll drop it under our Start function.
04:18 Ctrl+Paste, and let's take a look at it. Well, the first thing we need to do with
04:25 this guy is determine what speed he's getting, so we can report back to our
04:28 animator controller. We're going to get that by using agent.velocity.magnitude.
04:36 That's something we get when get when we put a nav agent component on an object.
04:42 The next three lines have to do with the character's rotation.
04:45 We won't be using them yet, but I wanted to go ahead and add them in here now.
04:50 Unity is going to take care of moving the character, and rotating him for us.
04:55 Finally, we're going to send off the turning angle to the animator controller
04:59 as well. So let's go ahead and click Save, and
05:03 head back to the editor. In my animator, I do need to give it that
05:10 extra parameter, so I'm going to add Float, and that will be named AV.
05:18 We aren't doing anything with it yet, but we will.
05:24 So now let's go ahead and switch back over to the scene.
05:27 Click Play, and now our character animates as he goes through it.
05:33 It'll be more interesting to watch our animator and see what that blend tree's
05:39 doing, so let's open our Animator window. And instead of this regular walk, go
05:46 ahead and put Bog Walk there, because that will really let us see when he goes
05:50 into the walk. It's not quite so subtle.
05:54 So we're going to go up here and find our good friend Bog Walk.
06:01 And load him in, and I'm going to go ahead and leave this open while we watch
06:05 it this time. So I click Play.
06:08 And now you can see as he slows down, especially right here, he goes into the
06:15 bog walk. And let's go ahead and stop playback.
06:21 So a single target's okay, but it's not very realistic.
06:25 So let's create a new little script and call it Nav Target.
06:28 Right-click, Create > Javascript, Nav Target.
06:39 And we'll open it up, and this one's going to be really short, but I'm still
06:45 going to go over to my Snippets folder and grab it.
06:52 Copy, and we'll paste it in here. And basically, this one is just going to
07:01 send a message on mouse down, so when we pick the object in the scene, it's going
07:05 to send a message that tells our other script, the Mec Agent, that this is the
07:08 new target. And then there's one more thing our Mec
07:14 Agent script needs, and that is the little function that will do something
07:18 with that information, so I'm going to Alt+Tab again.
07:25 And there's our SetTarget function. Copy, and we'll paste it down here.
07:32 And we need to save both of those scripts, and then we can head back to the editor.
07:37 I'm going to drag that new script, Nav Target, onto the sphere.
07:47 And he's going to need to know who the agent is, so I'll drag my guy into him.
07:52 And now, in the scene, we can select the sphere, Ctrl+D to duplicate him.
08:05 And add a couple more prospective targets.
08:11 Ctrl+D, I'll put this guy over here, and there's one more little thing we need to
08:18 do before we click Play. We need to head back to our Mec Agent
08:26 script, and comment out the destination from the Start.
08:34 So we can save the script. Head back to the editor.
08:39 And now if we click Play, we should be able to click to set him off to a goal.
08:47 Click again, and he'll run around to the next one.
08:53 We can interrupt him at any time. Let's go ahead and stop.
09:00 You probably noticed that if we click the original target first, the character goes
09:03 through one of the new ones. We could add a Navmesh obstacle component
09:07 to it, but then he won't come back to it as well.
09:11 But since this series is more about Mecanim, we'll just leave it as is.
09:14 We will, however, make one more Mecanim improvement to our pathfinding scene, next.
09:21
Collapse this transcript
Refining the PathFinding (Unity Pro only)
00:00 Int he last video we got our character running around the scene with the help of
00:03 Unity Pro's path finding system, and a simple Mecanim Blend Tree to drive his
00:06 animation as he moved forward through the scene.
00:11 This time we'll be nesting another blend tree into the original, so we can add
00:15 some turning animation. When we last left the scene, we weren't
00:20 doing anything with the angular velocity, so the character slid as he turned in the
00:24 Idle mode. This will be a perfect opportunity to add
00:27 another Blend Tree that will be controlled by a different parameter.
00:31 We added it earlier. It's the float type AV, AV for angular velocity.
00:37 In the script, we also already added lines in the animator move function to
00:42 calculate the turn velocity. Let's go back to the Mecagent script and
00:47 inspect it again. So we're looking at these last three lines.
00:53 I'm not even going to attempt to explain the math here but you can see that
00:57 getting the angular velocity is a lot more complicated than just getting the
01:02 speed was. The last line is where we send the result
01:07 of that. Back to our Animator Controller.
01:10 And I've used absolute value, because I'm just using a generic Turn in Place,
01:14 instead of a Turn Left, and a Turn Right animation.
01:19 In case you were wondering if there was more to that line, let's scroll over.
01:24 And that is the end of it, so we're okay on that.
01:27 So now we can head back to the Editor and set up our Nested Blend Tree.
01:31 So let's open the Blend Tree, double clicking, and select it, and we're going
01:37 to need to add not a motion field this time, but a new Blend Tree.
01:46 We're going to set its threshold to zero, since it's going to replace the idle, and
01:51 we can move it up to the top of the list by clicking and dragging it.
01:57 And now, we can delete our original idle. Now let's double click on our animator
02:03 view to get to our new Blend Tree. And it might be clever to name this one.
02:08 I'm going to name it Idle Blend Tree. And we need two motion fields.
02:18 One for the original idle, and one of walk in place.
02:25 For our threshold we need to set the angular speed in degrees...
02:36 But neither clip has any angular velocity.
02:39 Idle should have none of course, but the Walk In Place will be getting it from the
02:44 NavAgent script, through the AV Parameter.
02:48 We've got the AV Parameter in place, so let's go ahead and click Play and see
02:52 what kind of values it's reporting at the base of the ramp.
02:56 So we're going to be watching right here, as soon as we click to start things.
03:05 So it varies quite a bit, but let's say a value of ten should be fully walk in place.
03:11 So I'm going to uncheck Automate Thresholds, and put a value of ten in our
03:18 Walk In Place, and let's not forget to switch over to the av perimeter.
03:27 Now let's go ahead and click Play and check that turn at the bottom of the ramp again.
03:30 I'll select my target. And as he comes around, instead of
03:37 sliding, this time his feet stay on the ground much nicer.
03:43 If he still looks like he's sliding too much as he makes his turns in the Bog
03:47 Walk, we can also tweak its position in the original Blend tree.
03:53 Let's go back to that, and select it. The Bog Walk should be the second marker,
04:02 so if we drag it right, it will cause him to slide more, and if we move it left It
04:07 should tighten up that turn, from the Blend Tree.
04:13 So it's well worth the time trying to adjust the generated threshold values.
04:18 So in the last couple of videos, we've seen how speed can be used in conjunction
04:22 with path finding to animate the character, while the pathfinding does the
04:26 rest of the positioning for us.
04:30
Collapse this transcript
Using Mecanim with platform jumpers
00:00 In the last video, Unity Pros AI Path Finding System directed your character
00:04 and fed the right values into Mecanim to trigger the proper states and transitions.
00:10 In this video we'll be starting with the platform input controller script from the
00:14 Unity Standard Assets folder. It has a bunch of code to help the
00:18 character interact with typical platform jumper features but it has no code to
00:22 handle character animations. To save some time let's import the
00:29 platform unity package I've prepared And open the new Platformer scene.
00:37 Right-click, Import Package > Custom Package, and Platformer.
00:48 In the Scenes folder, You'll find our new Platformer scene, so let's double-click it.
00:53 And it starts out with Lenny. So now, from the Standard Assets folder,
00:58 let's locate the platform input controller script, and then drop it onto
01:03 the character. Standard Assets > Character Controllers >
01:09 Sources > Scripts, and here's our platform input controller.
01:16 We'll go ahead and drop it on Lenny. If I select him now, you'll see that
01:22 we've gained more than just the platform input controller.
01:27 We also have a character motor script and a character controller.
01:31 The character controller replaces the rigid body component and the capsule collider.
01:38 The first thing we need to do is set up the collider in the controller.
01:41 It's center needs to be up about 1. And we may need to lift him up a little
01:49 bit so he won't fall through the ground when we click play.
01:55 And he seems to be OK. The platform input controller needs a camera.
02:00 And the camera needs to be tagged as main camera.
02:04 If we select ours, we can see that's already been done, so we're good to go on that.
02:08 In the script, the camera influences the camera's direction, so let's make sure
02:13 the rotation is set to 0, 0, 0 for our camera.
02:19 And we're good to go. To simplify things, let's limit the
02:22 directions the character can go so it becomes a side scroller.
02:27 To do that, we'll need to open the platform input controllers script.
02:32 If we select Lenny, we can get to it quickly.
02:36 And open it up. It looks kind of scary, but at line 15 we
02:40 should find the lines that use the player input, and here it is right here.
02:50 So what we're going to do is we're going to substitute the vertical input with a 0.
02:59 And whenever I start changing someone else's code, I like to add a few
03:02 asterisks so I can find my changes if I need to.
03:06 And we can save the script. Back in the editor, we can click play and
03:11 drive the character back and forth with the left and right arrow keys.
03:17 And we outta try jumping him too. I'll click stop.
03:22 It looks like he's got a jet pack, so let's go ahead and adjust the jump in the
03:27 character motor component. Under jumping, I'm going to set the base
03:34 height to 2, the extra height to 0.1 and I'm going to give him a little more
03:40 gravity, and I believe that's under Movement.
03:48 I'm going to set it to 20. In the platform input controller, let's
03:55 make a few more changes. So let's scroll down here.
03:59 And for the mass rotation speed, since we're only letting him go two directions,
04:03 we may as well have him turn really quickly.
04:06 So I'm going to set this one to 1000 And let's click play and see what we've got now.
04:11 Send him back, and his jump looks much nicer, so we can go ahead.
04:17 Now, we're going to set up our animator controller to add the animations to the movement.
04:25 Let's create a new animator controller and name it platform controller.
04:32 And we'll do that up in the Controllers Folder > Create.
04:38 Animator controller, and I want this one to be platform controller.
04:44 And we'll go ahead and drag it on Lenny. As we saw in an earlier video since the
04:51 transforms will be fully controlled by the script, we need to uncheck Apply Root Motion.
04:57 And to start in our animator, we're going to need a float type parameter for our h input.
05:09 So lets call it H input. In the platform input controller script,
05:14 we're going to need the variables we need to communicate with the animator controller.
05:21 So let's head back to that. And then I'll head out to my Snippets file.
05:29 And select the next bit. Ctrl+C to copy.
05:35 And this is going to go up under the other variables.
05:38 Ctrl+V to paste. Give myself some space.
05:42 In awake, we need to assign the animator component, so I'll go out and get that.
05:48 Ctrl+C, we'll put it under the motor, control v, and at the top of the update
05:55 function, we assign the horizontal input to the h variable.
06:05 So I'll head out, pick up this guy, Ctrl+C.
06:10 And we want to put it at the top. Now because we've already assigned the
06:20 horizontal input, we can go ahead and replace, it's equivalent, where the
06:26 direction vector is assigned, like so. And finally, in the fixed update, we'll
06:33 send the h input value off to the animator controller.
06:38 If we wanted to, we could even make that an on animator move.
06:43 So head out, grab that, Ctrl+Copy, and since it's a function, we'll send it down
06:50 to the bottom, paste it in. I think I will turn that into an OnAnimatorMove.
06:59 And just like we did in an earlier video, I'm going to send the absolute value of H
07:06 so I won't have to add so many conditions in the Animator Controller.
07:16 So lets go ahead and save. And head back to the editor, and then in
07:20 the animator controller, we're going to make our first state, a blend tree.
07:26 Create state, from new blend tree. And I'm going to name this one base.
07:34 Because its a platform jumper, we ought to have him go directly into the run state.
07:43 But the problem is, when he comes down to a stop, he'll still going to be using his
07:47 legs in a run. So by using a blandetatry/g, we can use
07:51 our input to let him transition between idle, walk and run even though most of
07:56 the time he is going to be running. So let's go ahead and set up our blend tree.
08:03 And we'll give it three motion fields. In the first one, we'll put our idle.
08:12 In the second one, we'll put walk. In the third one we'll put our Run.
08:23 Now, we don't really want an even transition, so I'm going to uncheck
08:28 Automate Thresholds and what we're going to do is we are going to say he only is
08:34 going to walk if it's as slow as 0.2. So this way we're mostly running and then
08:42 we've still go a little transition down to the idle and that should help feet
08:48 slipping and running where they shouldn't be.
08:54 So let's go ahead and try them. Hit Play, send him off.
08:58 Stop him. And you can see he's doing a little bit
09:02 of a walk as he comes to the stop. In the next video we'll be doing the
09:07 jumps, because platform jumpers always jump.
09:12
Collapse this transcript
Adding a multistate jump
00:00 In the last video we come and do the unity standard asset script, platform
00:04 input controller and tweaked it to let Mecanim control the character animations.
00:10 So far we've got a run walk working well thanks to a Blend Tree.
00:15 Now because the script is totally set up for platform jumping, we need to add the
00:19 jumping part. The cool thing is, all we need to do is
00:23 to tap into the results from the script, to get our jump animation working.
00:29 We'll start with the jump in place. Back in the base layer, let's create a
00:34 new state with the B B just jump clip. Create state, Empty.
00:40 We'll name this Jump, and we're going to load in just jump.
00:53 And you'll notice we're using a clip that isn't going anywhere.
00:56 The just jump is the one goes up and kind of hangs there and then goes down.
01:01 The Mecanim team is apparently working on allowing us to use static poses with the
01:05 transitions but for now we need to make due with animations for the same purpose.
01:12 Just jump is only .5 seconds, but that gives us something to transition to.
01:16 So now, the question is what are the conditions?
01:20 Since the script is already jumping the character, we can see if his up, or y
01:25 velocity, is greater than .1. Since he could be travelling up on a
01:31 rising platform, we also need to make sure he's not still on the ground.
01:36 The script has a lot of complicated calculations to check for grounded.
01:41 And happily it has a grounded variable that we can tap into.
01:45 So let's start by adding two new parameters, grounded of type boolean and
01:52 Y Velocity of type Float. Now we need to open the Character Motor script.
02:00 That's where all the calculations happen. And then add our usual animator contact stuff.
02:08 So I'm going to open the Character Motor. Head out to my snippets file, grab our
02:18 variable, Ctrl+C, and then paste it up near the others, Ctrl+V.
02:29 And somewhere near the awake function, somewhere around line 180, we can assign
02:34 the animator in a start function of its own.
02:39 So I'll head out to my snippets file because the script doesn't already have a
02:43 start function we can add one. Control Copy, and we'll squeeze it right
02:51 in here, Control Paste. We probably could have put it in the
02:56 awake function with the rest, but it's okay like it is.
03:00 And in the update function, now somewhere around 339, we'll send back the value of
03:07 the grounded variable and the y velocity that the script has been tracking with movement.velocity.y.
03:18 I'll head out to my snippets again. Grab my last two lines, Control Copy, and
03:29 I'm going to put them underneath the existing code there, and Ctrl+V to paste,
03:41 and indent. So this is where we're getting the two
03:49 most important parameters. The grounded parameter, and, the y velocity.
03:55 So we can save our script now. Before we head back to the editor, lets
04:00 take a quick look through this script to see just how much work were being saved.
04:08 This is a pretty serious script and most of it has to do with jumping, and, is grounded.
04:15 Ok, so now we can head back. So now we can go ahead and set up our transitions.
04:20 So I'm going to make transition. And again, the transition into the jump
04:27 needs to be grounded equals false. Because the script has already gotten the
04:34 character jumping. So, grounded equals false.
04:39 He's already headed up. And we also want to make sure that his Y
04:46 velocity is greater than 0.01. Make sure he's on his way.
04:52 So, I'll add another condition, Y velocity.
04:58 Greater than 0.1. The exit transition can be why velocity
05:07 is less than 0.1. So let's click play.
05:12 And try pressing the Spacebar to make him jump.
05:15 And there he goes, and we'll stop. It works but it's not very convincing.
05:23 So lets create a landing state where he bends his knees as soon as he's grounded.
05:34 So we'll make a new state and we'll name this one landing and we'll load the
05:40 landing clip into it. The condition for landing is grounded
05:49 equals true. So I'll make my transition, set grounded
05:57 equals true and now I can delete my original exit here.
06:03 For the transition from the landing to the base, we can use the landing clip's
06:09 exit time. And it's already set to that so we're
06:14 good to go. So let's go ahead and click Play and see
06:17 how our jump is looking. Well we have a little problem on landing,
06:23 and that one has to do with that why they can't depose issue...
06:31 Most jumps, you want to uncheck it. But when we need him to come down to the
06:37 ground, this is one time when we don't want it checked.
06:41 So let's select, better box man B, locate the landing Clip, and this time we do
06:50 want Bacon to pose for the Y. Click Apply and Play, and we'll jump him again.
07:05 And this time, he lands. It's a little soft so I'm going to speed
07:14 it up. Now we can see one other issue, he should
07:18 really stretch his legs out to meet the ground instead of keeping them folded.
07:23 So let's add one more state going down, create state empty And we'll name this
07:30 one going down, and we'll load, guess what?
07:37 Going down. The transition from the jump To going
07:43 down, should be when the velocity is starting to get pretty low.
07:52 So we're going to make this one y velocity less than .1 and then we can
07:57 delete the earlier transition from the jump to landing.
08:05 And going down to landing will be the ground that equals true condition.
08:13 And now when we click Play and test our jump, it looks pretty decent.
08:23 So if you were thinking this was a lot of work to set up the jump in three states,
08:27 take a minute to adjust the jump height in the character motor component.
08:32 It's at two. I'm going to set it to three.
08:37 You might even try four. And if we click play and jump him You can
08:42 see, it looks just as good from 2 to 3, and it would for 5 as well.
08:52 While a canned motion capture jump would not have had the same flexibility, you
08:57 could take the time to split it up into 3 stages as well.
09:02 And use it the same way as we used our CG generated jump sequence.
09:08 Next, we'll need to take a look at the jump from the run state.
09:11
Collapse this transcript
Refining a multistate jump
00:00 Having sorted out the standing jump, now we need to check the jump from the run
00:04 transition, so I'm going to click play, back my character out of the scene, and
00:09 come in on a running jump. And you can see he slides when he lands.
00:18 So let's stop playback. It works pretty good until he lands, but
00:22 it's the landing clip that's letting him slide, so we really ought to set a
00:27 condition that he can only go into the landing if the h input is 0, or nearly so
00:32 since this is a float parameter. So let's select the transition, add a new
00:41 condition and this one is going to be H input.
00:46 And we want to make sure that it's less than 0.1.
00:52 So he'll almost be standing still before he can do this one.
00:55 And while we're there, we ought to turn off atomic, so it can be interrupted.
01:02 Now we need to get him out of the going down if the H input is greater than 0.1.
01:09 So I'm going to transition From going down to the base and that one will be H
01:17 input greater than 0.1, and we can give him a test run and see how it looks.
01:28 Click Play, back him out, come in at a jump and now he comes down...
01:38 And runs as he lands. So we're good on that.
01:42 For our final refinement, let's add a fall animation.
01:45 And with it, we'll be able to use our death zone to spawn the character back
01:50 onto a starting position. It would probably make sense to add it
01:56 from the any state using the y velocity. So I'm going to go ahead and create
02:01 state, Empty, name it Falling, and I'll go ahead and load my falling clip into it.
02:09 But before we can decide what velocity we need, we better give it a test run.
02:20 For that, I'm going to turn the ground off.
02:26 Before I click play, I need to turn off the death zone, since we haven't set it
02:30 up yet. The death zone is what's going to replace
02:33 the character back up onto the platform when we actually have our platforms in place.
02:38 We'll get to that in a minute. Now, if I click play and we watch the y
02:43 velocity, With Lenny selected. We can see that it bottoms out at minus 20.
02:53 And there's no coincidence that that happens to be the max fall speed, so
03:00 let's stop play mode And go ahead and make our transition from the any state,
03:07 to falling, and set its condition as Y velocity, less than let's say about minus ten.
03:21 Because we need to remember, that even in a regular jump, he's going to be coming
03:25 back down with the negative velocity. Now we can transition back to the landing state.
03:31 With grounded equals true. And finally we can go ahead and activate
03:37 the platforms in the scene. This is our first look at them.
03:42 They're nothing special but they do have animations on them.
03:48 You can find their animating scripts in the platform assets folder.
03:53 We have a horizontal position cycler in the x direction.
03:57 A horizontal cycler in the z direction. And I've also used a platform mover that
04:02 lets you put targets from one place to the other.
04:06 We also have a v position cycler. I haven't used all of them but I included
04:10 them in case you wanted to experiment afterwards.
04:14 So, now before we click play, we're going to need to turn on the death zone.
04:19 And if we look over in the inspector we notice we've got a problem here.
04:23 Mono behavior missing his script, and that's a fairly typical problem that
04:28 you'll come across with imported packages, so now I just need to drag the
04:32 death zone script into where it should have been...
04:39 An we also need to drag the character, Lenny, in as the character.
04:45 An in this little script, an we'll take a quick look at it here.
04:52 If we don't assign a spawn point, in other words, where he's going to come
04:55 back into the scene, it's just going to use the one he started out in.
05:01 So on trigger Enter, it's going to wait for 0.5 seconds.
05:05 And then it's going to take him back to the spawn point.
05:08 For that reason I'm going to head over to my scene view, select Lenny, and just
05:14 pick him up a little bit; that way he can fall back down on to the platform when he
05:20 gets spawned. Before I hit the Play button, though, I'd
05:29 better remember to turn the death zone back on, or Lenny won't be taken back up
05:33 to the platform. So now, let's go ahead and click Play.
05:38 And we'll run and jump and, and there he goes.
05:49 We'll watch him fall again, and as you can see, after a fall, Lenny is spawned
05:54 back up into his original position, ready to go again.
05:59 I'll go ahead and stop playback. So with the help of Mecanim, we were able
06:04 to take a very complicated script and add animations to it in a very short amount
06:08 of time.
06:10
Collapse this transcript
Interacting with objects
00:00 So far, we've spent most of our time moving the characters around using either
00:04 Scripts or the Animation clips with the help of User Input.
00:08 In this video, we'll be testing outside influences on the characters as they're
00:12 moving through the scene. We'll start by getting our character
00:16 bogged down as he wades through a marshy area.
00:20 Then he will have to go through an area where he will need to crawl, and finally,
00:23 he'll finish up with an area that burns his feet.
00:27 It's a fairly simple scene, but I've gone ahead and set it up for us.
00:31 So let's head out for this videos exercise assets folder and find the
00:37 Mecanim Environment package, Import Package > Custom Package > Exercise
00:43 Assets, and Mecanim Environment. Import, and then in the scene, we're
00:51 going to select mechanim environment. And I don't need to save.
00:58 In it, you'll find Lenny and three boxes that will be our zones.
01:09 There colliders haven't been set to his trigger yet so we'll go ahead and do that now.
01:13 Now let's open our test controller. We've already got a bog walk in the controller.
01:26 But we're going to need three different zones, so let's go ahead and make a new
01:31 sub state machine, and I'm going to copy my Bog Walk state, Double click my new
01:36 state machine, which by the way I'm going to name Specialty Walks...
01:43 Then I'm going to paste my original Bog Walk.
01:50 Then I'll go ahead and do that two more times.
01:57 The first one I'm going to name Fast Crawl.
02:03 And I'm going to load the Fast Crawl clip into it.
02:07 And the second one I'm also going to paste.
02:16 But this one, I'm going to use Bog Walk again.
02:18 But I'm going to set the speed to four. And we'll name it Hot Feet.
02:32 Now lets give each of the states transitions back to the idle state.
02:35 The Bog Walk already has a parameter. A boolean type.
02:45 It would be more useful to have an integer, so we could group our specialty
02:49 walks together. We already have a runtype, but it's a float.
02:55 So let's go ahead and delete the bog walk.
03:00 Go ahead and delete runtype. Make a new int run type.
03:10 This way we can assign the states 2, 1 for Fast Crawl 2 for Bog Walk and 3 for
03:16 Hot Feet. So for our transitions into the idle
03:22 state, we're going to set it when run type equals zero in other words its not a
03:26 specialty walk, and V Input is less than 1.
03:32 So Run Type equals 0 and V Input is less than 0.1, so it goes back to the Idle
03:40 state and we'll do the same for the other two.
03:50 Run Type equals 0, and V Input is less than 0.1.
03:59 And one more, Run Type equal to 0, and V Input less than 0.1.
04:16 Now, we also need transitions back into the Run Blend and that's going to be with
04:20 Run Type equal to 0. The input greater than .1, so I'm going
04:29 to create Transitions into the blend run, and those are going to be run type zero.
04:48 And v input greater than 0.1. And one more, Blend Run, Run Type equal
05:07 to 0. V Input greater than 0.1.
05:16 Back in the Base layer, and I'm double-clicking to get to it before I can
05:25 make my transitions. I really ought to get it somewhere where
05:35 I can see it, so I'm going to move it up so it's easier to deal with.
05:44 Now, I can make my transitions from the Blend Run into the Bog Walk into the Fast
05:55 Crawl, and into Hot Feet, and each of those are set up with their own number
06:05 for the Run Type. So Bog Walk will be Run Type 1, Fast
06:20 Crawl will be Run Type 2, and Hot Feet, looks like I missed.
06:38 I better try again on that one. Blend Run, Make Transition, Hot Feet.
06:45 There he is. And, Hot Feet, is going to be Run Type 3.
06:51 Now, we still got an extra Bog Walk from the earlier version of this.
06:58 So, I'm going to select it, and delete it, because our Specialty Walks are going
07:04 through the Blend Run. Since we need to get into the Specialty
07:14 Walks from the Blend Run, we need to make sure we're still in Race State 3.
07:20 And we are, so we should be in good shape.
07:25 Let's go ahead and check and make sure, so I click the up arrow and he goes on
07:29 through everything. Let's stop Playback.
07:38 And now, we need a simple script to change the Run Type variable when the
07:41 character enters and exits the bog zone. But since the script will be pretty much
07:46 the same for each of the specialties, let's make it reusable.
07:52 So let's create a new script and name it Zone Manager.
07:56 I'm going to put it up at the rest of the scripts.
08:01 Right-click, Create > JavaScript, and I'm going to name this one, Zone Manager, and
08:08 we'll go ahead and open it in Script Editor.
08:14 And it's going to need the usual contact information for the Animated Controller.
08:23 So I'll go over to my Snippets File and grab the variables we'll need.
08:33 Ctrl, Copy, Ctrl+V to paste. Next, we're going to need a way to
08:38 identify which specialty case should get used for the object the script is on.
08:47 Since the number requires you to remember which goes to what, we'll use an enum so
08:51 that the choice will be easier to identify in the inspector.
08:56 I'm going to go out to my Snippets File again, and grab my enum code.
09:08 Copy it, paste it in. And you can see the beauty of the enum is
09:14 that you can give a name to a number and then you can use the number elsewhere.
09:19 But the cool things is that these will turn up in drop-down in the Inspector, so
09:23 we can easily choose the one we want. And as usual, we're going to need to
09:30 locate and assign the animator in the start function, so I'll go out and get
09:35 that code, copy, and then I'll just paste it right over the original.
09:44 Ctrl+V to paste and then when the character enters the collider, we're
09:48 going to need an ontriggerEnter. Back in my Snippets File, I've noticed
09:53 I'm missing my ontriggerEnter, but I'll go ahead and copy my ontriggerExit and
09:57 with a few changes we'll be able to make it do double-duty.
10:03 So I'm going to copy and I don't need my update so I'm going to paste.
10:11 That one I'm going to turn into OnTriggerEnter.
10:16 Here's my real OnTriggerExit. So let's change this one to an Enter.
10:25 This one should say, Switch to current zone.
10:34 And for that, we're going to say, animator.SetInterger.
10:45 And we changed our Run Type from a Float to an Integer, so I need to change this
10:49 as well. So this one is going to be currentRun,
10:54 which is what is stored in our Run Types here.
10:58 And then our Exit, let's change him as well, SetInteger and that should be all
11:03 we need. If we wanted to be really thorough, we
11:07 could check and make sure that the object entering the collider was the character,
11:12 but we're just going to assign it manually, so we won't bother with that.
11:19 So let's go ahead and save the script, get back to our editor, and assign our
11:26 new one manager to each of the three zones.
11:32 Bog Zone, Crawl Zone, and Hot Feet Zone. Now, here's the beauty of enum.
11:44 I select my Bog Zone, and under currentRun, we're already set to Bog Walk.
11:52 So we're good. For Crawl Zone, we hit the drop-down and
11:57 select Fast Crawl, and for Hot Feet, we hit the drop-down and choose Hot Feet and
12:04 then each of them needs to know who the character is.
12:12 So I'm going to drag Lenny into the character for each one.
12:18 So now, we should be able to click Play and try running him through the zones.
12:26 We're in Race State 3, so he's running, so we're good.
12:32 So, we click Play. Pull the arrow down.
12:35 Okay. So, I ran him through and nothing happened.
12:40 Well, it turns back if I go back and I check my zones.
12:48 Apparently, I had turned off my box collider.
12:50 So I need to go to each one, turn the box collider on.
12:56 And now, when we click Play, and get him running, he Bog Walks through the first
13:02 one is expected, Crawls through the second one, and then Hot Feets his way
13:07 out through the third one. So let's go ahead and stop Playback.
13:15 For something more serious, you'd probably want to go back and adjust each
13:19 of the transitions as he goes in and out of the zone.
13:23 But we've got the basics working, and I think you've got the idea.
13:26
Collapse this transcript
Taking hits
00:00 In the last video, we created a passive means of affecting the character's state.
00:05 This time we'll be more direct. We'll cause something to collide with him.
00:08 And that normally means some sort of projectile.
00:11 Since we just need make sure we hit the player while setting up the conditions,
00:15 we'll set up the object to be instantiated slightly above the
00:18 character's location. We're going to need a few props, so let's
00:23 import the Projectile Package from this video's Exercise Assets folder.
00:26 Folder > Core Package > Custom Package > Exercise Assets, and Projectile.
00:42 We'll import. In the projectiles folder we got a couple
00:47 of new scripts, and a couple of Prefabs. We can see them in action in the
00:55 Projectile scene, so let's go ahead and open it.
01:01 No, we don't need to save. So let's click Play and hit the Fire1
01:06 button a few times. That's the left mouse button or the left control.
01:11 Click Play. And if I click it, you can see we get a
01:18 bunch of spheres. So our character was definitely getting bombarded.
01:26 Now, we'll take a quick look at the scripts to see what's happening.
01:29 Let's start with the Drop Projectile script.
01:33 It's pretty simple. When the player presses the Fire1, it
01:36 instantiates a projectile, at the characters xz location, and two point
01:41 meters above the ground. And then 3 seconds later, it destroys it.
01:48 So let's head back to the Editor, and next, we're going to need to come up with
01:54 an appropriate behavior for being beaned on the head with a sudo coconut.
02:02 I'm thinking the winded behavior will be just right.
02:05 We can click Play, and that one you may have set up to your Cmd key if you're on
02:10 a Mac or possibly your Right Ctrl key if you're on Windows like I do.
02:17 And if I click it, he drops down, so that will be just right for getting hit.
02:22 So let's open up the Basic Character Control script, and repurpose it for our hit.
02:30 My Mecanim Assets folder, Scripts, Basic Character Control.
02:37 And in the Update function, let's go ahead and comment out the Fire3 line.
02:45 That's whats currently triggering the wind.
02:46 So I'm going to comment that out for now. And the nice thing about it is we set it
02:51 up to call a function, and we can call that any time we want, so let's add
02:55 another function to tell us when the Character Collider gets hit.
03:00 I'll go ahead and copy it from the Snippets File, Ctrl+C.
03:06 And we're going to put this one inside the on collision enter, Ctrl+V to paste.
03:13 And so, what's going to happen is when our character gets hit the collision,
03:19 it's going to be checked, and if the object that hit him is tagged as a
03:24 projectile, which our little coconut thing is, then we're going to process
03:30 winded, just like we did with the Fire3 button before.
03:39 So let's Save. Get back to the Editor.
03:45 So now, when we click Play, and then click the Fire1 button to fire the
03:51 coconut, as soon as it hits him, he goes into that winded behavior.
04:00 So let's stop Playback. The problem, however, comes when it
04:04 bounces again. So if I click twice or three times too
04:07 quickly, you'll see that the skin mesh has ducked below the Collider that the
04:12 object is hitting. And I'm going to select Lenny, and now,
04:17 when I click, and if I keep clicking, you can see the projectiles are just hitting
04:22 the top of the Collider. At this point the best we can do is
04:27 resize the Collider's Height and center on hit, then reset it afterwards.
04:34 So let's go ahead and do that, but before we go any further, let's go ahead and
04:37 create a new Bool parameter named hit so we don't mess up a regular winded functionality.
04:43 So back over in the Animator, I'm going to create a new Glow, and we're going to
04:49 name this one, Hit. And we can copy and paste the existing
04:55 catching breath state and name it hit also.
05:03 So we'll just copy this guy. Paste him.
05:06 Rename it Hit. And we'll make our transition, which is,
05:15 when Hit equals true. The transition back to the Idle can just
05:23 use its exit time. The default.
05:25 So now besides handling the Hit parameter state we're also going to need to
05:30 manipulate the character's Collider in the script.
05:35 So the first thing we'll need to do is declare the variables that we'll need to
05:38 access the Collider's parameters. I'll go out and grab those from the
05:43 snippets file, and then we'll be putting them in the basic character controller again.
05:48 So I'll grab the variables we need, Ctrl+copy.
05:55 And we'll put them up near the top. And then we're going to need to assign
06:04 them in the Start function. So I'll grab those lines and copy and
06:13 then our start function, I'll go ahead and add it after here Paste, Indent.
06:21 So basically, we're just assigning the current values of the capsule Collider in
06:28 this one, because we'll need those to get it back, and now we're going to make a
06:34 new function to handle the hits and we'll call it manage hits.
06:44 So I'll go out and grab that one. Copy, put it down at the bottom here.
06:51 And it's kind of full, but basically what it's doing is, as soon as we trigger
06:56 manage hits, we're going to set the hit value as true, we send that off to the
07:00 animator and then right away we set it as false.
07:06 So it doesn't keep re-triggering. And then, we're going to re-size the
07:11 height and the center of the Collider. So this should pop it down because we're
07:17 timesing it by 0.75. And then we'll wait for 7 seconds, and
07:22 yeah, I could have used the Exit Time or something of the clip, but 0.7 was fine.
07:29 And, after that time, we're going to go ahead and reset it to its original.
07:34 Original Height and Original Center. So there's one more thing we're going to
07:38 need to do, we're going to need to change the call to winded in the on collission
07:43 inter function to manage hits, and I'll just go ahead and copy that.
07:50 And here, instead of ProcessWinded, now we want a ProcessHits.
07:59 Well, ManageHits, and paste. And get rid of the extra parentheses.
08:05 So we Save. Head back to the Editor and let's make
08:11 sure we have gizmos turned on and Lenny selected.
08:18 Now, we click Play, and when we click you can see the Collider changes size.
08:26 So if I hit a couple of times, it's a lot more realistic.
08:30 Okay, let's stop Play mode. So the Collider doesn't match the
08:34 character exactly, but it does give you a rough means of matching the characters
08:39 Collider with his animation. For better control, Unity Pro users will
08:46 be able to animate the Collider's height with curves, and we'll see that in the
08:49 next chapter.
08:52
Collapse this transcript
7. Animation Extras in Unity Pro
Working with curves
00:00 In the last video, we set and reset the character's Collider when one of the
00:03 parameters was triggered. But it was an all or nothing sort of solution.
00:08 With Unity Pro, we gain a few extra Mecanim features, including one that can
00:12 help us with the fine control needed for some auxiliary animations.
00:18 While the majority have to do with IK and Transform Targeting, the one we'll start
00:22 with is Curves. The most important thing to understand
00:26 about Curves is that they are just that. They are curves that you can get the
00:30 values from to feed into other parameters.
00:35 Ideally, we need a clip that doesn't loop, changes height, and is long enough
00:39 to see the action. That pretty much means we'll be using
00:44 Full Jump. We'll trigger it from the Hit State we
00:47 created in a previous video. So let's go up to the
00:51 BasicCharacterController script and make the few modifications.
00:56 In the OnCollisionEnter, I'm going to make a ManageHits too, then I'm going to
01:04 copy our ManageHits, and paste it. And basically, I'm going to take out all
01:13 the stuff that was manipulating the Collider, because we're going to do that
01:17 differently this time. So there's just a stripped down version
01:24 of it. And, I better remember to name it to.
01:27 So now, instead of ManageHits, we're going to be calling ManageHits2.
01:35 Save the script, and head back to the Editor.
01:40 But start by temporarily assigning the Full Jump to the Hit State.
01:48 So instead of Out of Breath, we're looking for Full Jump.
01:55 And we should probably select Lenny, click Play, and go ahead and bean him by
02:03 clicking the Fire1 button. And you can see right now that the
02:11 Collider is going up with him. So let's change that first off.
02:19 This is one time when we're going to want to use Bake into Pose the y transform,
02:23 even though this is a jump. So I'm going to select BetterBoxManC, and
02:29 Full Jump is the first one in his list, and let's look and see what we've got.
02:35 So, the first thing we want to do is Bake into Pose, and that's going to keep the
02:39 Collider down for us. If we click Apply before Play, we won't
02:45 get a message. Now, I'll click Play > Select Lenny so we
02:51 can see it. This time if I click, now he jumps out of
02:54 his Collider. He also lands properly, which is one of
02:58 the reasons why we want to do this. The last time without the Bake into Pose,
03:02 he sort of floats up and down. So our mission here is to grow the
03:06 Collider, as he jumps up. So say if he was in a platform jumper and
03:12 he had a power up up above him, he would need a Collider to hit it, not his skin mesh.
03:20 So that's what we're going to work on. So to access Curves, we need to do it
03:25 through the actual clip we're using, so once again, I'm going to select my
03:29 BetterBoxManC, and select the Full Jump there.
03:34 And at the bottom, or nearly at the bottom of the panel, you'll find Curves.
03:41 We're going to click that Open, and we're going to click the Plus button, to create
03:46 a new Curve. And I'm going to name it something that
03:51 is meaningful to what it's going to be doing, and it's going to be affecting the
03:55 Collider's y height, so I'm going to name this Collider Y.
04:00 And so I've just created a little curve. I might give myself some more real
04:05 estate, too. And this is one time when we're going to
04:09 need to use the Preview in its original location.
04:17 So what we need to do now, is we need to script through the timeline in the preview.
04:21 And we're going to add keys that Critical points.
04:26 So I'm going to slowly scrub through the timeline, and I'm going to say, okay,
04:30 well, he's crouched down here. So I need a key here.
04:34 So I'm going to click the Add Keyframes, and that sets a key for me.
04:40 I'm going to drag it again. And let's say this is probably the next
04:48 place we need a key, so I'm going to click there, and he holds that till
04:54 about, probably that there, so we'll click on there.
05:04 And then he's down, and, there's his low point again, so I'll click another one there.
05:14 So we've added our keys. Now, this doesn't look like a typical
05:17 Curve Editor, but we can click on it and it opens up and gives us a regular Curve
05:21 Editor like we've used in the Shirk and Particles, and the audio and a few other
05:25 components, but I'm going to go ahead and close it again.
05:30 Because the next thing I want to do is I want to use my step through keys, go to
05:36 previous to start adding my values. Now, the other thing we need to know
05:44 about this is that these are normalized values, so we're going to be affecting a
05:47 percent of the Collider. We're not giving it an absolute value.
05:53 So, let's leave this at 0% change at frame 1.
06:00 Actually, it's not a frame because time is also normalized.
06:03 So, this represents 1 unit of time or the time of the clip.
06:07 So, we'll go to number 2, here. And I want to set this one to a minus
06:13 0.75, because I want it shorter than its Collider.
06:17 So, minus 0.75. I want to go to the next key by using the
06:23 step next key again. And, this one needs to be taller, so this
06:30 one I'm going to make 1.25, so it'll be 125% of the original height.
06:38 Hit Enter. And now I'm going to go to the next key.
06:44 And that one also needs the same value. You kind of have to watch out for these,
06:51 they don't always catch it correctly. And then the next key is back down to our
06:57 minus 0.75. And that's going to be kind of rough, but
07:03 you get the idea. When we're finished, we need to click
07:07 Apply to save the changes. Now, having created the Curve, we need to
07:14 head over to the animator and create a matching float parameter, Collider Y.
07:20 So we make a new parameter and that's going to be a float.
07:27 And make sure it's spelled exactly the same or it won't be able to receive information.
07:38 Okay, so we're off to a pretty good start here.
07:40 Now, we're going to need to get the script getting the values from the curve,
07:43 so they can be used to change the Collider's height.
07:48 There's two things we'll need to know for the script.
07:51 One., when the Hit State or Transition is active.
07:54 And two, what the value of the Curve is during that slice of time.
08:00 We'll also need to contact the Collider component.
08:04 But since we did that in the previous chapter, we're already set and good to go.
08:09 So now, we just need to let the Curve do the work.
08:13 And that code is going to go into the fixed update, so it gets accessed evenly
08:17 over time. Let's go ahead and get back to our Script
08:20 Editor and the BasicCharacterControl script.
08:24 And I'll head out to my Snippets, copy the code, Ctrl+C, and in our Script
08:35 Editor, we're going to add this to the fixed update.
08:47 There it is. And I'm going to go ahead and put it near
08:50 the bottom of that.Ctrl+V, and so, let's look at what we've got.
08:55 So this is one where we need to find out if the layer is active or not and so
09:00 that's what this is. We've seen this before.
09:04 We already know that we've got access to the layers, because that's in the first
09:09 part of our fixed update function. If our Hit State is the active one, then
09:14 we're going to adjust the Collider's height with the original height plus
09:18 whatever we get from that Collider Y parameter, which is picking it up from
09:22 the Curve. So now, I didn't get fancy, I didn't
09:27 adjust the center, like we did on the other one, so it's going to be a little messy.
09:35 But the main thing to know is that it is going to work off of that curve.
09:39 So let's go ahead and save the script, head back to the editor,make sure we
09:44 select Lenny and you can also watch the value of the Collider Y parameter change
09:50 as it goes through the Curve. So let's click Play.
09:58 And when I pick him, now you, the jump is kind of a mess but you can see what it's
10:03 doing is it's actually using the values from that curve.
10:10 To control the height of the Collider and we'll stop play here.
10:16 So finally, the Collider gets higher as the character jumps up, and it comes back
10:20 down with him depending on how accurate our curve is.
10:25 You might think it might have made more sense to raise the Collider's center so
10:28 it would just move up. But because, we're using gravity in the
10:32 rigid body component it would have dropped him back down through the ground.
10:36 We could of course turn gravity off and on, so as you see with curves involved
10:41 solutions can be complicated, so it pays to think them through thoroughly.
10:47
Collapse this transcript
Targeting with inverse kinematics (IK)
00:00 In this video we'll be taking a look at the other unity pro feature, IK targeting.
00:05 For those of you not yet familiar with the term IK, or inverse kinematics, it's
00:09 the process of positioning an IK chain, like an arm, but moving the end effector,
00:13 usually the wrist or palm. Instead of using FK, forward kinematics,
00:20 to rotate each joint into position individually.
00:25 Unity Pro allows us to access foot and hand IK and if you search for avatar in
00:29 the scripting hill you'll see a bunch of other options as well.
00:34 We'll be testing with the hand .Let's start by saving the current scene and
00:41 renaming it the IK test. File > Save Scene As, make sure I get
00:48 into my Scenes folder, and we're going to name this one IKA test.
00:56 And we don't need the coconut or the coconut dropper so I'm going to delete those.
01:01 And we may have left the race state to three, so let's make sure we check And
01:06 make sure that it's not, okay. Make sure we set it to zero cause we're
01:11 going to want him to walk in this scene. In fact we're going to want him to walk
01:14 pretty slowly so I'm going to select my walk state.
01:17 And I'm going to slow it down to point seven five for now.
01:27 Next, let's duplicate the simple input control script, control + d, and rename
01:30 that one Simple IK Control. And we'll need to replace the basic
01:34 character control script with it on our character.
01:38 So I'll drag it onto him, but I need to remember to remove the previous one,
01:49 Right-click > Remove Component. Let's go ahead and click Play and make
02:00 sure it still works. Play, we should be able to drive him
02:05 forward slowly and backwards, and we can turn him, but he doesn't animate, but
02:09 that's fine. So let's stop Play mode, and next we're
02:15 going to need to make a goal to move his hand towards.
02:19 So let's switch over to the scene view. Give them some lights here, and I'm going
02:26 to go ahead and focus on him. And make a new game object, create other
02:33 And we can create a sphere, and I think we want to scale it down to about, maybe, 0.25.
02:43 That'll be a pretty good size. And we want to move it out in front of him.
02:54 A little bit off to the left, his left, and just out of arms reach.
03:05 Probably at about shoulder high. I'll try that and see, and let's also
03:12 disable it's collider so the character can get close to it.
03:17 So we'll uncheck the sphere collider there.
03:20 Now we need to add a little bit of code, so let's open the newly duplicated
03:25 script, simple IK control, and the first thing we're going to need is a variable
03:30 to hold the target. I'm going to head out to my snippets,
03:38 Ctrl+Copy, and I'll add it in here, Ctrl+V for paste.
03:47 Before we add more, we really ought to have a short rundown on how IK weighting works.
03:52 With IK you can set a weight to dictate how much the IK influences the position
03:57 of the IK location on the character, and this goes from 0 to 1, so 0 is no
04:01 influence, 1 is full influence. Changing the weight is how the control of
04:08 the limb is passed from the state machine to the IK solver...
04:13 So let's create another variable to control the weight of the limbs position.
04:23 And we'll stick that one right underneath here, Control V, so it's a float and it's
04:27 going to go from zero to one... Actually it can go beyond that but it's
04:32 not going to count past one. Next we're going to need to use the Unity
04:38 Pro on Animator IK callback, so now we need, I'll go out to my snippets for it, Copy...
04:52 It's a function, so I'll put it down at the bottom here, where I have space.
04:55 Paste, and right now, I've just got it printing here.
05:02 I like to have a print statement so we can tell when the call back is entered.
05:05 So inside the callback, under the print statement, the first thing we need to do
05:13 is set the weight and paste. And let's look at this one.
05:22 So animator.SetIKPositionWeight. And we start with the, animator IKGoal.LeftHand.
05:30 Well that's one of the ones you get to use.
05:33 This is something you need to check your scripting help for.
05:36 And then, weightForPosition. And that's one that we made, the variable for.
05:41 So that's for position. And right after that we need to tell it
05:46 where the goal is. So I'm going to go out and get that code.
05:54 Control copy, control paste. And this one, SetIKPosition, same IK
06:01 goal, the left hand, and for this one we're using our target objects position,
06:07 so here's our weight that we are feeding it and here's our target position.
06:16 If you've looked at other examples for IK you'll see people using avatars the
06:20 variable name. Instead of animator, like we've used
06:23 before to contact the animator component. In the early days of Mecanim, component
06:28 was named avatar. But since Unity decided to replace their
06:32 legacy animation system with Mecanim over time, it was renamed to Animator so it
06:36 wasn't so character-centric. With I K where the goals are directly
06:43 concerned with the characters, the avatar name was retained.
06:46 So lets go ahead and save the script. Head back to the editor, and now we need
06:53 to assign this sphere as the target object.
06:57 So lets select Lenny. Here's where we need to add the target
07:01 object so we're going to drag this sphere into it.
07:09 And I'm going to switch back over to the animator, and click play and if we adjust
07:16 the weight all the way up to one, or past it, we can see that nothing's happening yet.
07:26 So there's one more thing we need to do. If we check the console for the message,
07:31 we can see that we've had no message reported.
07:35 That means that we didn't even enter the on animator IK callback.
07:40 Turns out we need one more thing. Let's open the test controller in the
07:44 animator view and add a new layer. Make sure that we're on test controller,
07:49 that's what we're using here. Click to make a new layer, and this one
07:56 let's name Activate IK because... We scroll down a little ways, we'll find
08:07 a check box for i k pass, and that's what we need to have activated.
08:13 Now if we click play, and adjust the wait.
08:23 Lenny's arm reaches out towards the goal. And if I scrub it back to zero, it goes
08:28 back down. With a weight of one, let's go ahead and
08:32 try to get him walking back and forth to see what happens.
08:37 I'm going to switch over to the scene view, where we've got a closer view of him.
08:46 Click Play. Once again, we're going to move the value
08:50 up to one and this time I'm going to move him forward, and you can see his arm
08:55 folds up. Move him backwards.
09:00 And if I move him to the side his arm still tries to stay out at that goal...
09:10 So it's his arm, or rather his wrist, which is the location of the IK solver,
09:14 and that attempts to stay on target. Location, though, is only half the picture.
09:20 We need rotation to enable the character to properly put his hand into position to
09:23 grab an object. Or speaking in Game Code terms repair the
09:28 object of the character's hand bone. So let's add another variable for the
09:34 rotation way to our to our script. I'll head out to my snippets, control 'c'
09:39 and it's a variable again, so we are going to add it here and in the on
09:44 animated 'IK' function we need to add the position's rotation counter parts.
09:54 And I'll grab these as well. Ctrl+C, and I'll go ahead and put them
10:00 beneath these. So now, you can see they're pretty
10:06 similar, except for it's rotation rather than position.
10:09 And I'll go ahead and save the script. Get back to the editor, and this time
10:17 I'll need to make both weights one. So I'll click Play, and I've got two weights.
10:26 And I'll just type in one this time. And now if I move character forward and
10:32 backward, his hand keeps a correct rotation.
10:41 So, there's one more thing we can try here.
10:42 And that is, in the scene view, now that he's at 1 for both weights, we can try
10:50 adjusting our target. And you can see how his hand continues to
10:57 try and stay at the same rotation. So, now if I rotate my target.
11:04 It also rotates his hand. So you can see how if you wanted a
11:07 character to put his hand in position to grab something, you'd need to determine
11:12 the exact the position and rotation to set the target at.
11:17 And since iCade does not guarantee a good solution, you'd either need to make sure
11:22 his orientation was close or force him into the correct location in orientation manually.
11:28
Collapse this transcript
Looking at targets
00:00 In the last video, we experimented with (INAUDIBLE) to be able to direct the
00:04 character's feet and hands, to specific locations and rotations.
00:09 In this video, we'll have a look, no pun intended, at a weighted look at.
00:15 So let's continue with the IK test scene, and add a few more things.
00:19 First we'll need a look at target. So let's create a small cube in front of
00:22 the character. Game Object>Create Other>Cube.
00:31 And I'm going to shrink that down quite a bit.
00:38 I'll go ahead and pull it out in front of him.
00:40 And then we need to add the code to make the look at happen.
00:44 So let's open up our simple IK script again.
00:47 And give it a couple of new variables, and I'll grab a couple of new ones here,
00:52 Ctrl+Copy, and I'll add them under the others.
00:56 So we've gotta look at Object, which is going to be the goal, and just like the
01:01 others we need a variable for the weight. In the on Animator IK function, we need
01:08 to add the code for the look at. So I'll go out and get that, Ctrl+Copy,
01:17 and I'll add it under the others and paste.
01:24 The set look at weight is the interesting one here.
01:28 This top one. The weight variable, that's pretty easy
01:32 to recognize. But then we got a whole long string of numbers.
01:35 All of the values are clamped between zero and one, or 100% of the possible
01:40 movement range allowed. The first argument is weight, of course,
01:45 it globally determines how much weight the look at gets.
01:49 So you will use it to ease in and out of the lookout.
01:52 The first three, body weight, head weight, and eye weight, let you determine
01:57 how much of the individual components you want.
02:02 And the last one is clamp weight. It lets you decide how much of the range
02:09 you want to use, but this one, zero means no clamping or full range and one means
02:14 fully clamped for no range or movement. We'll be experimenting with that one to
02:20 see exactly what it does. We'll also set variables for these in a
02:25 minute, but first let's just use the numbers we've got.
02:29 So let's save the script, head back to the editor, and assign the cube as the
02:40 new look at object. As a default, we left the look at weight
02:48 at one. So it should be fully active when we
02:51 click Play. And now, you can see him looking at it,
02:55 and as I pull it up and down his whole body goes with the flow to watch it.
03:09 So it's a pretty powerful little feature. Lets go ahead and leave it on the ground,
03:15 and adjust the weight in the Inspector. I'll select Lenny again.
03:25 And here's our Look At Weight, and I'll take it down to zero where he doesn't see
03:30 it at all, and back up to one or past one till he's fully interested in it, and
03:34 let's stop Play. To test the other parameters, we'll need
03:41 some variables, so let's head back to the simple IK control script and add them.
03:46 Ctrl+Copy, then we'll add our extra variables, which we'll paste.
03:58 And here's that body weight, head weight, eye weight, and clamp weight that just
04:01 have numbers right now. So we're going to need a version of the
04:06 set look at weight with all those variable names.
04:10 I'm going to leave this guy as is, so I'm going to comment him out, and then I'll
04:15 go out and get my full variable version, Ctrl+Copy.
04:21 And I'll put it under this one, Ctrl+V, and we'll probably need to scroll, or
04:29 actually it all fits. We're good.
04:34 So now let's actually go ahead and save the script, head back to the editor.
04:43 But before we test, it'd be really nice if we had some animation on the cube, so
04:48 I'm going to grab the HZ position cycler script from the platformer folder, and
04:53 drop it on the cube. That was one of the ones that cycles back
04:59 and forth with a simple sine curve. Plat-former assets, HZ, and I'll set it
05:07 to about one meter in either direction, and I'll go ahead and click Play.
05:24 it looks like it should be the x direction.
05:26 Let me just swap that out. So the x position cycler, and I'll just
05:31 drop it in here, and it retains the numbers.
05:37 So now it should go sideways. that's much better.
05:41 Okay, now we're going to select Lenny. And We're going to adjust these weights
05:46 one at a time, so I'm going to start by decreasing the body weight to zero, so we
05:52 get less there. I'll run it up again so you can see it
05:59 gradually go up and down. Okay, so there's no body weight.
06:07 Now I'm going to decrease the head weight.
06:09 And now he's got no head weight. And if he actually had eyeballs, we could
06:15 then also Prevent his eyeballs from following it.
06:19 Let's stop for a minute. And let everything get reset.
06:29 With our values reset, let's click play again and adjust the clamp.
06:36 And Nothing seems to be happening. Let's go ahead and leave it at one
06:42 though, fully clamped. And, now we are going to rotate the
06:47 character 90 degrees. I'll go ahead and do it through the
06:50 Inspector here. And as I rotate him around To 90 degrees,
06:57 now he has a really hard time looking at that cube.
07:09 But if I change my clamp weight, watch what happens.
07:12 So I'm going to bring it down slowly back to, well, zero.
07:20 At zero, he contorts painfully to keep watching it.
07:25 At one, he hardly does anything. So, the default 0.5 is kind of a nice,
07:35 happy medium. So now I think you can see how weighting
07:42 with IK and look at's could cut way back on specialty animation clips.
07:47
Collapse this transcript
8. Working with Generic Rigs
Setting up generic rigs
00:03 The characters.
00:03
Collapse this transcript
Making miscellaneous character adjustments
00:00 In the last video we took a peak at using generic characters with Mecanim.
00:05 Since characters can vary widely it's always worth running some tests to see
00:08 just what constitutes a generic character.
00:12 We've got a few other characters to test, so I've already set up their animations,
00:16 controllers and scripts to save time. So let's import the generic pre-fabs
00:22 package from this video's exercise assets folder.
00:25 Right-click > Import > Package > Custom Package.
00:32 And here it is. Import, and let's see what we've got.
00:40 Close up a few of my folders. And in the pre-fabs folder, We have a new
00:45 folder called Generics. Let's start with the quadruped, Kitty.
00:52 I'm going to drag Kitty Generic into the scene.
00:55 And there she is. If we click Play, we can see that she
00:59 immediately has an orientation problem. So I'm going to select her in the
01:08 original folder. Kitty and in the animations we need to
01:14 look under root transform rotation. Instead of root node rotation we're going
01:24 to need to change her to original... And we can also bake in depose, and we
01:33 need to do that for both of her animations, so I'll select the walk,
01:38 change it to Original, they can depose, and click Apply.
01:46 Now when we click Play, her orientation remains correct, and she also, well, sort
01:54 of walks. So, she's okay.
01:58 When she was walking you might have noticed a little glitch.
02:04 It, by the way, is not in the source files.
02:07 It's a side effect of the way that Mecanim handles the hips, or pelvis as
02:12 the root. Before we check out the other characters,
02:16 there's one more thing to consider with this quadraped It's a quadruped, yes, and
02:20 it has its own animations, but if you think about it, apart from the tail
02:24 bones, the skeleton is really no different from a humanoid.
02:30 So just for fun, let's see what happens if we try her out as a humanoid.
02:35 Remapping the bones was painful, so I've prepared a prefab for us to look at.
02:39 That's kitty 2, and in rig, humanoid, we'll look in configure, we'll go ahead
02:47 and save the scene, and after I got all of the bones remapped this is what she
02:54 looked like. So she did fit into humanoid.
03:04 Let's click Done, and return to the scene, and drag her in, because there's
03:08 actually another problem we'll run into. So I'm going to drag the prefab kitty
03:14 humanoid into the scene right next to the kitty generic.
03:23 And when we click play, you'll see that there's definitely a problem here.
03:29 When the other characters have an animation on their pelvis or hips, their
03:34 feet stay in place, which hers do, but the problem now is, that everything above
03:40 the hip is allowed to rotate. And that's a mechanim thing, so it looks
03:47 like we can't get away with making a quadruped from a humanoid after all.
03:53 Let's try the mech character next. He's got no head, so he won't qualify as
03:57 humanoid, although you could give him a head bone if you wanted to.
04:02 Let's go ahead and drag his prefab into the scene and click Play.
04:06 He's got the same orientation issue since he's also a cat rig.
04:11 And he looks okay for a couple of strides.
04:16 Let's check out this walk clip in the original animation.
04:23 So I'm going to go into animations, and I'll increase this.
04:29 Make sure we're in walk, and I'll click Play.
04:36 And you can see, he loops quite nicely. So then stop, and let's see what happens
04:45 in the scene itself. I'm going to back up a little bit so we
04:48 can see more than a stride or two. And if I click play, and press my forward
04:55 key, he goes forward. But you see that little jerk when he
05:01 steps back there. That's a problem that we don't see in the clip.
05:06 The animation is okay, but the transform itself is off.
05:12 So it looks like I've just got one too many frames included in his walk cycle.
05:17 So I can fix that pretty easily, and I'll try dropping it back and see how it looks
05:26 after I click Apply and press forward. And that fixed it.
05:40 So sometimes it's just a simple frame count fix.
05:45 I've also included the spider just so you can see a multiped in action.
05:48 He's also a cat rig but I've also fixed his orientation issue.
05:53 So we'll drag him into the scene just for a second here, spider prefab since he's
06:00 already set up, click Play, and when I click Run, there he goes.
06:11 Finally, there's one more little thing I want to show you.
06:15 Let's select Hoppy and look at his hop cycle.
06:17 In Animations, I'm going to click on Hop, enlarge my window a little bit here, and
06:25 I want a good side view of him here. When I click play, you can see that his
06:36 knee bends back painfully, and I'm pretty sure he wasn't like that when I animated him.
06:45 There's one quick test you can do to see if it's the animation, or if it's
06:49 something that Mechanim's doing. And that is, we can duplicate the asset,
06:55 and set his Rig type to Legacy, and see what happens.
07:01 So in the project view, I'm going to Ctrl-D to copy him, and then the the
07:06 inspector, this guy, I'm going to set to legacy, the original animation system.
07:16 Generation is set to store and renew, which is what we want.
07:20 Click Apply, go over to Animations, and you can see he's, the clips are already
07:24 set up, so didn't lose those. But now, if we drag him into the scene
07:30 notice what legacy you actually need to drag a model into the scene to see it preview.
07:40 And so now I've got him showing, and let's look at him in a side view.
07:48 And if I click play you can see that this time his knee behaves like it should.
07:54 So you do have to watch out for Mechanim and some of the things it can do to your animation.
08:00 If he was a humunoid you could probably fix it in the muscle section where you
08:05 can limit the range of the limbs. But sometime you're not going to have a
08:11 choice so the best thing to do would be to just go back and adjust the original
08:14 animation, so the bottom line here in our exploration of generics is be sure to do
08:18 preliminary test with the characters before investing too much time in them.
08:25
Collapse this transcript
9. Placing a Character in a Game Environment
Setting up a character for the game environment
00:00 In this video, we'll be setting up the character to run a little obstacle course
00:04 that brings it all together. We shouldn't need to do too much to our
00:08 Test Controller, but we'll be adapting the First-person Controller Script to our
00:12 character to take advantage of a lot of its functionality.
00:15 Let's start by deleting the characters in the miscellaneous characters scene, and
00:21 then let's rename it to Game Test. Now I need a character, and I'm going to
00:30 go ahead and choose my just Box Man. You can choose whatever you like, but
00:36 make sure he's only got an animator controler on him.
00:40 Because he's going to get the combo rigid body capsule collider that we've seen
00:44 earlier, the character controller component.
00:47 So all he should have right now is the animator component.
00:51 Now let's go ahead and duplicate our Test Controller, Ctrl+D.
00:59 And we'll name this guy Contestant Controller.
01:05 Since we'll have access to the scripts grounded in y velocity values, we'll need
01:12 to add those as parameters. So let's open it up, add our parameters.
01:23 Grounded is a boolean, and we need a float for Y velocity and over here I'm
01:31 going to reset the walk that I've changed in an earlier video.
01:45 We slowed it down for the IK movie So let's set that back up to 1.5.
01:52 And since we'll have our velocity now, let's have our jumps exit from Y velocity
01:59 less than .2. Just like we did in the platformer.
02:07 In fact, you'll probably want to go and fix up these jumps like we did in the
02:11 platformer and maybe even add a falling state like it had as well, so my exit
02:18 from my dynamic jump is also going to be y velocity less than 0.2.
02:29 Let's hold off adding our new controller to our character.
02:32 In fact, let's make sure that our character's Controller is listed as None.
02:37 If you have one there, go ahead and click the Browse and choose None at the top.
02:44 Next we'll need some scripts so let's import the Game Scripts folder from this
02:49 video's exercise assets. From Exercise Assets, I'm going to copy
02:59 the folder and go into my game Assets folder, and paste it.
03:10 And as soon as we get back into Unity, it's waiting for us in the Project View.
03:18 In it you'll find our mechanim versions of the character motor script, the FPS
03:26 Input Controller script and a stripdown version of the mouse look script.
03:33 I've already added the code for the scripts to be able to communicate with
03:36 the Animator. And I've also added my triple asterisks
03:40 to the places I've added to the scrips in case your curious.
03:46 So let's go ahead and add the mechanim character motor script to our character.
03:52 It will also need to add a Character Controller component from the physics
03:58 section, and remember this is the one that combines the rigid body and the
04:03 Capsule Controller. So we'll need to set up the capsules position.
04:11 And depending on your character it's height, and it's radius.
04:17 There we go. Now let's add the Mecanim Input
04:23 Controller, this is what gives us the x c input key controls for forward, backward,
04:28 and strafe. And we can do a quick check.
04:38 So I'm going to click play and use my arrow keys.
04:46 And sure enough, that's what we get. Also you're going to see that error
04:49 message down in the console here, but don't worry about it.
04:52 It's just looking for the controller that we haven't added yet.
04:59 So the one thing this doesn't do is turning.
05:01 So we're going to be using the Mouse Look script to turn.
05:05 It's a much better solution for guiding the character in a more varied environment.
05:10 So we'll go ahead and add our Mouse Look script, and now if we play, as soon as I
05:19 move my mouse, he turns, and now I can move him around With a combination of both.
05:26 At this point, you're probably going to want to use W and S to move him, rather
05:30 than the up and down arrows, unless you're left-handed.
05:33 So our character quickly heads off without us.
05:37 So the next thing we're going to need to do is set up a camera to follow him.
05:42 So we have a camera in this scene, so we're going to select it, and from the
05:47 component menu > camera control, we need smooth follow.
05:51 And this one defaults to a distance of 10, which is a little long, so I'm going
05:59 to set it to 8. And before we click play we need to
06:03 remember to assign the box man as his target, or whatever character you're using.
06:08 So now if we click play, the camera comes in behind him, and with our W key and the
06:19 mouse moving, we're going around nicely. Now we should probably try a jump.
06:30 And there we go. Stop playback, and I'm going to go ahead
06:35 and adjust my jump. So I'm going to select my character.
06:40 And in the mecanim character motor, under jumping, I'm going to set this to three.
06:46 I'm going to try three or four. And this one maybe to about 0.1 and under
06:52 movement, I'm going to set the gravity to 20.
06:57 Now, lets go ahead and add our Contestant Controller.
07:00 And see about getting our animations hooked up to the action.
07:05 So I'm going to drag my Contestant Controller onto him.
07:12 And let's click play and see what we've got.
07:16 He's turning nicely, he's picking up his feet, although we might want to speed it
07:20 up a little. And if I go forward, though, he's
07:24 obviously getting extra speed on top of his walk.
07:33 So, previously, we've let the script run the speed, but this time, we're going to
07:38 let Mecanim take care of it, so we're going to turn off the speeds in the
07:43 motor's movement section. So in our Mecanim Character Motor script,
07:50 I'm going to set my speeds forward, sideways and backwards to zero.
07:59 Now, let's click Play and test it. And with W, he walks forward.
08:04 So that's good. Let's go over to our Animator and set our
08:09 Race State to 3, so we can check on his Run.
08:14 Click Play, and there he goes. Looks like I've got a little Bog Walk in
08:23 there that I shouldn't, so I'm going to stop real quickly and fix that.
08:26 In my blend run, I can see I've got two hot feet here.
08:31 Could have sworn I only need one, so I'm going to click on the bottom one and it's
08:37 showing exit time, which is definitely not The one I want, so I'm going to
08:41 delete it from the list there. Now let's click Play.
08:46 Much better. And jumps, and if we stand still, he jumps.
08:55 We're pretty well set to go except for one more thing.
08:59 Let's turn off Play here for a minute. When we switched from the input keys to
09:05 control the turns, we also changed the values.
09:09 So really quickly, let's watch our turning parameter and look and see what
09:13 the values are on it, so if I do a regular turn here.
09:18 You can see it's probably not getting over about .5 or less than minus .5.
09:25 So we really ought to go over to our blend run and change our thresholds.
09:36 So we get our full turn on that. Otherwise it's never getting to a full
09:39 one or minus one. So we're never getting our full turns.
09:43 So I'm going to set those to about 0.5 each, minus 0.5 and 0.5.
09:50 Now, if we click play, you're going to notice we're having a little issue down
09:57 here on the console. It says, Animator has not been initialized.
10:01 Don't panic. If we clear the inspector, it goes away.
10:10 So now here's my turn at the stand. I run and turn and you see he's a lot
10:14 more responsive now. If you want more response on that turn.
10:20 That one's being handled by the mouse look.
10:25 So you can change your sensitivity down here.
10:27 I'm going to leave mine as it is though. So with our character all set up to use
10:33 the first person shooter controls while still letting Mecanim control the
10:37 movement from our animation clips. We're finally making the most out of
10:41 Mecanim without giving up the functionality we're used to having from
10:45 our input control scripts. Now it's time to bring our character into
10:50 the Game Environment.
10:51
Collapse this transcript
Testing the character in the game
00:00 In the last video we finished setting up the scripts and animator controller for
00:03 our game character. So lets start by creating a prefab forum
00:07 in the prefabs folder. And lets name it game character.
00:13 Create>Prefab. Key in character and we'll drag him on
00:20 into it. Now we need our game environment, the
00:28 obstacle course, so let's import package, custom package, and from the exercise
00:36 assets folder, we'll bring in game environment.
00:47 In the Scenes folder, we'll find the game scene that was just imported.
00:54 And it's called Game Scene Base. So let's go ahead and Double-click it, to
01:00 bring it in. And I'm going to Zoom in on it.
01:09 So our little course starts up out at the top here.
01:12 Our character's going to run down through this area, he is going to cross a rickety bridge.
01:18 He's going to come in here and serpentine through some barrels.
01:22 If he's lucky, he makes through the bog zone without getting bogged down.
01:28 Then he comes around here, cuts through a rock tunnel and then goes to the hot feet section.
01:35 Has to watch out for a rock fall. And then he'll finish up over here in
01:40 this little clearing. So let's start by focusing in on the
01:43 start gate. And then we can drag our new prefab game
01:50 character into position. We'll probably need to spin it around a
01:55 little bit. About a minus 60 or 80 somewhere around
02:05 there should be good. And next we'll need to select the main camera.
02:16 Camera Main. Then from Components, add a smooth follow
02:24 to it and drag our game character in as his target, and again I'm going to adjust
02:32 the distance to about eight. Now we've got a few zones that we can set
02:43 up using our zone manager script. They're going to need both the character
02:48 assigned to them, and we also need to select the right speciality walk for each
02:52 one, so I'll go up to bog zone... And drag my zone manager onto it.
02:58 We're going to assign bog walk to it, drag our character in.
03:05 Then we'll put our zone manager on the crawl zone.
03:10 Set it to fast crawl. Drag our character in.
03:21 And then, we also have a Hot Feet Zone. Put it to Hot Feet, drag our game
03:30 character into it. And then we have one more zone.
03:37 We have a Death zone. And, so if we head back to our platformer
03:44 assets folder, we can get our death zone script and drop it onto it.
03:50 And it's going to need our character and this time if we fall off that bridge it
03:55 would be much nicer to start back near the bridge instead of the starting point.
04:04 So we've also got a spawn point here. You' can find that in the bridge group.
04:08 So we'll drag spawn point into there. Before we head out to test it, it's worth
04:12 pointing out that our specialty walks are not set up to return to idle from the
04:16 race state, so that means our character is going to keep going even if we let up
04:21 on the forward key. So it's a good thing to keep in mind here.
04:27 We'll also occasionally get some extra velocity from the script, so navigation
04:32 isn't perfect yet, but it's a nice test for our Mecanim character.
04:38 So let's go ahead and click play and test it out.
04:43 So I'm going to start him out, my timer starts, and if I don't make my turn here,
04:48 I'm going to end up down in the death zone which brings me back up, that's okay.
04:55 Now I can go over my bridge. I've got colliders on the side so I won't
05:00 keep falling off. Here's our serpentine area.
05:05 And you could set up colliders to check points or accuracy.
05:09 In here, here's the tricky part. I need to make my jumps or I end up in
05:15 the bog zone and can't make any time. And there I finally jumped him out.
05:25 So now, we're going to head down to the rock tunnel, hard left, my other left, he
05:32 drops down to the crawl and he comes back up.
05:38 And now he's in the hot feet zone. And then we head into the rocks.
05:51 You can see the rocks start to fall. But I made it through.
05:58 And finally he comes to the finish. Now that'd obviously be a good place to
06:02 trigger the out of breath or winded, behavior, but I haven't done that yet.
06:08 You could easily, though. So there's lots of opportunities to add
06:11 spectators that use pro features look at functionality as the contestant goes by.
06:16 And they could also be waving, or some other layer type behavior.
06:21 The gates all have colliders that will allow you to keep score or triggering
06:25 other interesting animations. And we didn't get a chance to incorporate
06:29 our start sequence, but the framework is there for you if you wish to try it.
06:33 So this wraps up our Mecanim course, both the game and the Lynda course.
06:39 I hope you enjoyed it and have a better understanding of the power that Mecanim
06:43 brings to your Unity projects.
06:45
Collapse this transcript
Conclusion
Next steps
00:00 Having just finished this course, you may be interested in trying Mecanim with some
00:03 motion capture animations. Unity's Asset store has a couple of free
00:07 Mecanim scenes that contain a large number of mot cap clips that are freely
00:11 available for your use. If your interest takes you more toward
00:15 the modeling end, be sure to check out preparing your own character from the
00:19 Unity user manual. Along with asset preparation and import.
00:25 They will provide you with a lot of tips to help with problem free importing.
00:30 Unity supports models and animations from several different applications.
00:34 For more detailed information on the application of your choice, see the how
00:38 do I import objects from my 3-D app. From the manuals FAQ.
00:45 And finally, if you need help creating your own characters, rigging them, or
00:49 creating your own animations to use with Mecanim, lynda.com has a large variety of
00:54 courses that may help you on your way.
00:58
Collapse this transcript


Suggested courses to watch next:

Unity 3D 3.5 Essential Training (10h 51m)
Sue Blackman


Game Character Creation in Maya (2h 58m)
Chris Reilly


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,069 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,025 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked