animations not working

i try to trigger a animation with the g key and it works the first time but the second time it doesn’t do anything please help i will give any code or information you need to figure it out please help

Without giving us any information about the animation and the code it’s hard to tell you why this is happening.

Are you using a simple Animation, or are you working through an Animator?

I’m just trying to guess here, but if you’re using the Animation component and you have something similar:

if (Input.GetKeyDown(KeyCode.J))
        {
            animation.Play("My animation");
        }
}

Your problem is that if you play a second time an animation, it’s played resuming from the last frame of the animation it had been played.
You can fix this by calling the Rewind function like this:

if (Input.GetKeyDown(KeyCode.J))
            {
                animation.Rewind("My animation");
                animation.Play("My animation");
            }
}

Consider that, this way, if you press again the J key before the end of the animation, you will restart the animation, getting probably an unwanted effect where the animation is restarted before it can end.
You could check if the animation is not playing before making the player able to play the animation from the beginning:

if (Input.GetKeyDown(KeyCode.J) && !animation.isPlaying)
            {
                animation.Rewind("My animation");
                animation.Play("My animation");
            }
}

I am not certain about these since I can’t test right now:

  • In your Animator graph, you can make sure your animation has “Has Exit Time” clicked, and that animation points back to a default animation (like an Idle animation) to launch from. This will automatically set it up to play again.

OR

-Use animation.Play(“AnimationName”, -1);
This will reset the animation to the first state and play it.

Please help fix this problem i reALLY NEW