How To Play Animation Through Script? - Unity

I am making a game and I made an animation for the win screen. According to all the tutorials I've watched, the animation is played when you activate the GameObject. However, it plays at the Beginning instead. I think the solution would be to play it separately when the game wins. Could you show me how to do that, please? Thank you in advance (:

1 Answer

When you create an Animation for a Game Object it's added as a State in the Animation Controller (Animator).

Unity Animator Overview

To Call a specific animation:

Option A:

You can use the Animator Component to play an animation at a specific event

animator.Play("StateName");

Option B:

You can use Animator Parameters (Trigger or Boolean) to play the animation

animator.SetTrigger("TriggerName")

animator.SetBool("BoolName",boolean value)

In Order to use the Animator Parameters, you need to define the parameters and assign them to a specific transition

In this example Once the Boolean Die is true the Animation will occur (And in the script , you use Animator.SetBool("Die",true) once the health is zero)

Unity Animator Conditions

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like