Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (:

like image 872
JMasterBoi Avatar asked Apr 16 '26 06:04

JMasterBoi


1 Answers

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

like image 137
Ahmed_Schrute Avatar answered Apr 17 '26 19:04

Ahmed_Schrute