How to use 1 Key for 2 animations being played individually one after another

Hey Guys, to put this simply I am trying to have an animation where the player presses F to enable an animation where their hand holding a torch comes to frame; once F is pressed again the same animation but in reverse is played to put the torch away.

The issue im having is that pressing the same key again either repeatedly plays the torch coming to frame or simply bugs out between both animations playing.

Any suggestions on how to Get the first animation to play by pressing F and then getting the second animation to play by pressing F again (then to restart so you can simply put the torch in and out of frame as and when you want).

The animator and script is extremely simple on my end so far so any suggestions would be amazing!

That just sounds like a bug. But before you go too deep into debugging…

Anything with Animations / Animators / Mechanim:

Only consider the code AFTER you have done this critical step:

Always start with the Animator state machine and prove it works in isolation, no code at all.

Here’s more reading:

NOW… once you are 100% sure the animation side of things is perfect, move to the code.

If it still doesn’t work then it is time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Hey Bud, I really appreciate the swift help on this!

my animator seems to be working how it should do (Press F to hold out the torch<doesn’t restart>) but when i press F again i cant get it to play the 2nd animation where it puts the torch away.

ive attached images of the animator and the simple script to show some more detail.


After a few hours i have managed to figure this out.

If you are looking to be able to do something like for example have a first person game but want to include an animation where you see your players arm lift up an item (of which is in frame of the Player camera) then return to being out of frame then here is how i have done it.

(ensure you have your animations made and ready - Intermediate and some beginners should understand this process; if not there are plenty of tutorials on Youtube.)

Animator:

  1. Transition your entry box on to an empty state box (This will loop no animation).

  2. have your first animation (the one coming into frame ) above the “New State Box” and have no transition connected.

  3. Have your second animation to the right of the “New State Box”. Transition the second animation to this “New State” Box. (Ensure that “Has Exit Time” on this transition is checked.

Code:

public class (ScriptName) : MonoBehaviour
{
    bool firstMouseInput = true;

    Animator anim, IsPlaying;
    
    void Start()
    {
        anim = GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.<Your Key Of Choice>))
        {
            if (firstMouseInput)
            {
                FirstMouseInput();
                firstMouseInput = false;
            }
            else
            {
                SecondMouseInput();
            }
        }
    }

    void FirstMouseInput()
    {

        anim.Play("Animation1");

    }

    void SecondMouseInput()
    {
        anim.Play("Animation2");
        firstMouseInput = true;
    }
}

Essentially what is happening here is you are saying "I want my the first time i press i want it to do “Animation1” but the second time i press it i want “Animation2” to be played.

setting it back to true at the end essentially refreshes this script and allows it to be looped (starting back at Animation1)instead of doing animation 1 and 2 then just playing animation afterwards.

I WOULD SUGGEST FINDING OUT HOW LONG YOUR ANIMATION LASTS FOR AND SET TIME DELAYS THAT ARE EQUIVELENT IN EACH MOUSE INPUT OTHERWISE IF YOU PRESS THE KEY QUICKLY IT WILL LOOK BAD BY RESTARTING THE ANIMATION AGAIN BEFORE IT ENDS.

I really hope this helps people who want to use the same key when using in other animations :slight_smile: