CTIN 289 - Interactive Snippets Library

Powered by 🌱Roam Garden

Bygones Animation


This method is useful if you have a character or object that has a decently complex animation system that needs to be triggered by bools/events. Start by adding an animator component to the object and dragging in all animations into the animator window like this:

Next in the script:

Reference the animator, then set up references for all the animations.

private Animator animator;

// Reference to all animations in animator

    string currentState;

    const string PLAYER_IDLE = "Idle";

    const string PLAYER_RUN = "Run";

    const string PLAYER_JUMP = "Jump";

Next, have a function for changing animation states.

// For changing animation states

    void ChangeAnimationState(string newState)

    {

        // stop same animation from interrupting itself

        if (currentState == newState) return;

        // play animation

        animator.Play(newState);

    }

Now you can call to change animation state in code anytime. This can be nested in if statements or wherever.

ChangeAnimationState(PLAYER_RUN_SCARED); // play run scared animation