Hey everyone,
I’m hoping this is a simple thing and its just me being bad at coding.
I have a few objects with multiple animations on them (5 each)
What I want to do is a on mouse down script play the animations in order storing what was last played so that the first time you click it plays 1 and stops then if you click again it will play 2 etc.
And that it would store what each was at so you could click on the other objects as well in between and each would be at their own state between the 5 animations.
I know i can do 1 animation with this but i want to cycle through to 5 and have it stop at 5 and not go back to the beginning.
function OnMouseDown () {
if(gameObject.name == “object”)
animation.Play(“t1”);
}
Thanks for the help and sorry if this is answered somewhere else I so far have been unable to find it.
Maybe someone could chime in on a better way as I am still fairly new at Unity, but what I would do, in your Animator setup an Int parameter. Then setup conditions on which animation to play based on what the int value is making sure to return them back to your default animation after “exit time”. Then in your script, keep track of the clicks, and update the Parameter from your script using something like:
animation.SetInt("ParameterName", numOfClicks);
Why not store the number in the script and recover the correct animation with it (from string or array)? Something like:
int currentAnim;
function Start() {
currentAnim = 1;
}
function OnMouseDown() {
if (currentAnim <= 5) {
// Assuming your animations are named accordingly
animation.Play("t" + currentAnim);
currentAnim++;
}
}
A nicer way to do that would be to store the animations in the Resources folder to be imported at runtime in an array and referred to by their index (currentAnim).