-1
\$\begingroup\$

The states and animations not my own.

I have a prefab of a character that have Animator component attached to it. The character in the Hierarchy have Animator with some states and blends and also have some animation clips.

Now when i play/run a State of the Animator is that meaning i'm playing animation? Or the animation clips is something else ?

And why i can't playing the animation clips ? I'm not getting errors or exceptions it's just not playing it.

Here is a script that attached to this character and i also added to this character a Animation component. So now the character object have both Animator and Animation components.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class SwitchAnimations : MonoBehaviour
{
    private Animator animator;
    private int index = 0;
    private static UnityEditor.Animations.AnimatorController controller;
    private UnityEditor.Animations.AnimatorState[] an;

    // Use this for initialization
    void Start()
    {
        animator = GetComponent<Animator>();
        an = GetStateNames(animator);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            animator.Play(an[index].name);
            if (++index == an.Length)
                index = 0;
        }
    }

    private static UnityEditor.Animations.AnimatorState[] GetStateNames(Animator animator)
    {
        controller = animator ? animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController : null;
        return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
    }

    private void RollSound()
    {

    }

    private void CantRotate()
    {

    }

    private void EndRoll()
    {

    }

    private void EndPickup()
    {

    }
}

Inside the variable an there are 9 states:

Aiming
Death_A
Use
Grounded
Roll
PickupObject
Reload_Rifle
Shoot_Rifle
Empty

When i click on A key it's playing the states one by one each time i click on A Everything is working fine i can play the 9 states by clicking on A.

what i don't understand with the States is inside the editor when i'm in the hierarchy selecting the character and in the menu make: Window > Animator i see the 9 states on the LegsLayer:

9 States

But then if i click on state Grounded double click i see a blend tree and i'm not sure if this is also states or maybe this is animation clips ? Then how can i get access to them ?

Blend Tree

Ok this is one part i don't understand yet about the Animator and what and how i can play with it.

The second part is related to AnimationClip/s and why i can't play them ?

The script for animations clips:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

public class SwitchAnimations : MonoBehaviour
{
    private Animation animation; 
    private AnimationClip[] animationClips; 

    // Use this for initialization
    void Start()
    {
        animation = GetComponent<Animation>();
        animationClips = AnimationUtility.GetAnimationClips(gameObject);

        foreach (AnimationClip clip in animationClips)
        {
            animation.AddClip(clip, clip.name);
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            animation.clip = animationClips[6];
            animation.Play(animationClips[6].name);
        }
    }

    private void RollSound()
    {

    }

    private void CantRotate()
    {

    }

    private void EndRoll()
    {

    }

    private void EndPickup()
    {

    }

    private void FootStep()
    {

    }
}

And in the variable animationClips i have 9 animations:

Rifle_Aiming_Idle
Rifle_Aiming_Walk_F_RM
Rifle_Aiming_Walk_B_RM
Rifle_Aiming_Walk_L_RM
Rifle_Aiming_Walk_R_RM
Rifle_Idle
Rifle_Walk_F_RM
Rifle_Run_F_RM
Rifle_Sprint_F_RM

When i'm running the game and clicking on A this time i see in the Inspector inside the Animation component all the animation clips and in Animation i see the animation clip to play.

I'm not getting any errors or exceptions but it's just not playing it. The character stay still idle. Even if i choose another animation clip index to play 5 or 3 or 1 nothing is playing.

I wonder why it's not playing the animations when i click A. In this case animation number 6 from the List but still not playing.

Animations in inspector

\$\endgroup\$
  • \$\begingroup\$ The general idea is: In the editor you cteate animations. Then in the editor you assign an animation to each animaTOR state. From there in code you change the animaTOR states to control what animation is played. \$\endgroup\$ – Nikaas Aug 30 '17 at 2:58
1
\$\begingroup\$

An Animation Component is good for when you want just one or a handful of animations on an object, with very simple transitions you're happy to control entirely in code. Things like:

  • A spinning coin
  • A UI element sliding onto the screen, pulsing, and reacting to a click
  • An animated prop like a machine that just runs in a loop, or can be turned on & off
  • A firing animation on a weapon that doesn't need precise coordination with a character's hands

It acts like a tape player: You load up the cassette (AnimationClip) that you want it to play, and tell it when to play, pause, stop, or change to a different source. It doesn't do anything particularly clever on its own, just plays the animation start to finish (or optionally looping).


An Animator Component is good for when you have complex layered animation with many states and transitions that would be fussy to wire up entirely through code. Things like an animated character who:

  • Can move in multiple directions according to player input
  • Needs to blend between forward, turning, and strafing animations
  • Needs to blend between different movement speeds (idle, walk, run...)
  • In multiple movement modes (crouched, upright, climbing...)
  • While transitioning in & out of ability animations (throwing, aiming, firing...)
  • And layering with character states (injured, carrying something...)

Controlling all of this manually in your own code would get brittle and repetitive, and it would make it hard for an animator on your team to tune the blend weights and timings for the right look.

So instead the Animator Component wraps all of this transition and blending logic in something called an Animation Controller state machine. This expresses all of these details as data in the form of flow charts, so it's quicker to iterate on, and already contains reusable patterns for common animation tasks like blending, layering, and masking.

This makes working with an Animator Component different than the tape cassette model described above. One of the great things it does for us is abstract away the individual animation clips into states, so our code doesn't need to know about the individual clips at all or micro-manage when to play each one. That frees the animators on your team to completely change the look of a character's animations, and do fancy things like blending multiple clips, or playing a different run clip when the character is hurt or encumbered, without breaking or creating new dependencies in your code.

So instead of telling the Animator Component what clip to play, the usual approach is to tell it what's happening, by setting state and trigger variables on it. From there its internal state transition logic will find the right animation or blend of animations to serve that state.

This will usually look something like:

myAnimator.SetFloat("Forward", Input.GetAxis("Vertical"));
myAnimator.SetTrigger("Die");

(You can of course cache those strings as integer IDs for better efficiency)


You rarely, if ever, want to use both an Animation Component and an Animator Component on the same object. Unless you're very careful to divide their activity and responsibilities, they're likely to fight for control of the same objects and you might get nonsensical results or no animated movement at all.


To fix your specific case:

  1. Remove the Animation component so the two don't fight
  2. Re-import your animations - it looks like you set them to legacy animations when trying to get the Animation component to work, and that animation type is not supported by the Animator, as the warnings in your screenshots are telling you
  3. Try controlling your Animator component using the variables and signals on the Parameters tab of its controller graph.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.