I was wondering about launching a simple animation triggered by a button.
For example : my scene has a simple cube with two animations ( Take 001 is a translation animation, and take 002 is a rotation animation. ), one canvas, three buttons attached ( One button “Translate”, one button “Rotate” and one button “Quit”. ), one empty GameObject on which I linked my scripts.
In my Animator Controller, named Cube_AC, I have the two states Take 001 and Take 002 linked on Any State and I created two triggers named respectively GoTake001 and GoTake002 for the transitions.
I tried different ways to launch any of the two animations by script but I never succeed ( I tried to link some script in the OnClick section of the button but the animation won’t play. )
All that my scene is doing is playing the two animations one after another ( my default state is Take 001 ). But when I click on the button nothing happens.
Have some kind of ideas on how I could do?
This is just a test scene, I want to build some kind of 3D player in Unity.
I’m scripting in C# and i’m a real beginner.
Thanks a lot for the help!
Maybe you need to set up your animator to have two different states, attach Take001 to one state and Take002 to the other one, add transitions between them and set conditions for your animation transitions from one state to another within your animator.
“Any state” I think cannot be end for any transition based animations. Here’s the explanation for any state state:
By default the animator always create the transitions conditioned by “Exit Time” that’s the reason that the states could be executing one after another.
Here’s a bit more detail about how to do that:
1-Create a Paramater for your animator:
2-Create a transition between your 2 states
3-Change the transition to be controlled by your parameter
4-Whenever you want the animator to switch states, add a trigger within your game logic (could be a collision, a button click, a key input detection) and change the value to this parameter
var anim = gameObject.GetComponent<Animator>();
// Sets the value
// Gets the value
bool isInConga = anim.GetBool("ParamName");
Here’s the complete tutorial in case you need it: