Chapters

Hide chapters

Unity Apprentice

First Edition · Unity 2020.3.x LTS Release · C# · Unity

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

13. Advanced Animation Principles
Written by Ben MacKinnon

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last chapter, you enabled Chef to go about his business in the kitchen. With the help of the tween library, ingredients drop into the trough now. Chef can pick them up, wash them, chop them, place them on the plate and serve them.

But Chef himself is stuck in the same pose throughout.

Fortunately, Chef came prepared with his own set of animations!

Open the starter project for this chapter, then open the Kitchen scene from Assets / RW / Scenes.

Take a look in Assets / RW / 3D / Chef / Animations and you’ll find a collection of animations that were imported with the 3D model. Click the arrow to expand one of them, then select the icon that looks like a moving triangle.

You’ll be able to play a preview of the animation in the Inspector window.

You have animations for all the following states:

  • Chop
  • Idle
  • Idle-Hold
  • Pick Up
  • Walk
  • Walk-Hold
  • Wash

In this chapter, you’ll learn how to build up an animation state system using the Animator Component, including how to transition between states, and different ways to trigger the transitions.

Animator component

In the previous chapter, you created an animator controller for the arrow scrolling animation. It triggered the scrolling animation, which remained in the same state forever. However, the animator controller is a lot more powerful than that.

If you recall, the animator for the arrows looked like this:

By the end of this chapter, you will have created an animator that looks like this:

First things first. You need to create the new animator controller. Navigate to Assets / RW / Animations in the Project view and select AssetsCreateAnimator Controller from the main menu bar. Name it Chef_Animator.

Then, in the Hierarchy view, assign this new animator controller to the Animator component on Chef under Player / Chef.

Save the scene. Chef is now ready to be animated.

Importing the animations as states

The animator controller is currently empty. Well, it has three default states: Entry, Exit and Any State (depending on your editor layout, you might have to zoom out a bit to see all three). These states alone don’t provide any animations. So the first task is to create some states that Chef does need.

Animator transitions

Notice there’s already a transition arrow from the Entry state to the Chef_Idle state. In order for the state to change throughout the animator controller, you need to set up transitions in each possible direction so that one state could go to the next.

Animator parameters

In order to change the states in your animation, you need a way for the user input that’s already captured in code to be passed to the animator controller. As you know from the previous chapter, there’s already code in here to make Chef walk around and pick up ingredients. So all you need to do is add a connection from those pieces of code to the animator controller.

Float Condition

Add a float parameter and call it Speed. Then, select the transition between Chef_Idle and Chef_Walk. In the Inspector, find the Conditions list and add a new condition. The new Speed parameter will be selected by default. Set the value to Greater than 0.1.

private void FixedUpdate()
{
    if (moveInput.enabled)
    {
        Vector3 move = 
            new Vector3(-moveInput.ReadValue<Vector2>().y, 
                        0,
                        moveInput.ReadValue<Vector2>().x);
        if (move.magnitude > 0.01f)
        {
            Vector3 targetForward = 
                Vector3.RotateTowards(transform.forward, 
                                      move, 
                                      6.238f * Time.fixedDeltaTime,
                                      2);
            controller.Move(playerSpeed
                            * Time.fixedDeltaTime
                            * move);
            transform.forward = targetForward;
        }
        else
        {
            controller.Move(Vector3.zero);
        }
    }

}
animator.SetFloat("Speed",
                  controller.velocity.magnitude / playerSpeed);
private Animator animator;
animator = GetComponentInChildren<Animator>();

Boolean Condition

Now that you’ve seen how to set up parameters and transition conditions, and how to change them from the game code, it’s a straightforward process to toggle Chef’s holding animations.

// state for holding something or not
[SerializeField]
private bool holding;

animator.SetBool("Holding", holding);

Animator triggers

The Chef animations are almost complete. But if you remember from when you first looked at them, you have two more that have yet to even be added to the animator controller. Chef needs to be able to Wash and Chop the ingredients. Unlike the walk and holding states though, these are actions that are triggered and then play out for a set duration. This is where the Trigger parameter and Has Exit Time come into play.

public void SetAnimationTrigger(string name)
{
    animator.SetTrigger(name);
}

Challenge

Sometimes you may have an animation that would work either in forward or reverse motion. Using the Speed variable on an animation state, you can vary how that animation plays in the game. For a challenge, try adding another state for Chef_PutDown and add it into the animator controller for when the user puts an ingredient onto the plate or puts the plate onto ThePass.

Key points

  • Animator controller controls different animation states.
  • Animation States hold animation clips.
  • Transitions describe how you move from one state to another.
  • Parameters can be used as conditions to transition between states.
  • Has Exit Time means that a transition will trigger once an animation has finished playing.
  • Parameters Float, Int and Bool are values that can be set from code using animator.SetFloat, animator.SetInt or animator.SetBool respectively. Conditions can compare these values to see if a transition should trigger.
  • Trigger is a special parameter that describes a condition that happens immediately when you ask it to via animator.SetTrigger.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now