In this recipe, we'll learn to animate the hat of the Unity bean-man character in response to a jump event.
To animate body parts for character movement events, follow these steps:
BeanManAssets
, by choosing menu: Assets | Import Package | Custom Package …, and then click the Import button to import all these assets into your Project panel.10
.-30
. Set 2D gravity to this value by choosing menu: Edit | Project Settings | Physics 2D, and then at the top change the Y value to -30
.0
, 3
, 0
).0
, -4
, 0
).Samples
), and choose menu item [Create New Clip]:0.01
Current State
Jump
(click plus-sign '+
' button at bottom)0.5
(this needs to be the same time value as the second keyfame of our Jump animation clip)0.01
Current State
You have added an Animation controller State Machine to GameObject character2D. The two Animation clips you created (idle and jump) appear as States in the Animator panel. You created a Transition from Idle to Jump when the 'Jump' Trigger parameter is received by the State Machine. You created a second Transition, which transitions back to the Idle state after waiting 0.5 seconds (the same duration between the 2 keyframes in our Jump Animation clip).
Note that the key to everything working for the bean-man character is that when we make the character jump with the SPACE key, then code in the PlayerControl
C# scripted component of GameObject character2D, as well as making the sprite move upwards on screen, also sends a SetTrigger(…)
message to the Animator controller component, for the Trigger named Jump.
The difference between a Boolean Parameter and a Trigger is that a Trigger is temporality set to True
and once the SetTrigger(…)
event has been 'consumed' by a state transition it automatically returns to being False
. So Triggers are useful for actions we wish to do once and then revert to a previous state. A Boolean Parameter is a variable, which can have its value set to true/or False
at different times during the game, and so different Transitions can be created to fire depending on the value of the variable at any time. Note that Boolean parameters have to have their values explicitly set back to False
with a SetBool(…)
.
The following screenshot highlights the line of code that sends the SetTrigger(…)
message:
State Machines for animations of a range of motions (running/walking/jumping/falling/dying and so on.) will have more states and transitions. The Unity-provided bean-man character has a more complex State Machine, and more complex animations (of hands and feet, and eyes and hat and so on, for each Animation clip), which you may find useful to explore.
Learn more about the Animation view on the Unity Manual web pages at http://docs.unity3d.com/Manual/AnimationEditorGuide.html.
3.144.252.204