- Home /
[Solved] How to animate multiple objects once I click on one of them
Dear Community,
once again I am in dire need of your help. Being fairly new to scripting, to Unity, everything really I can't seem to solve this on my own. I am very sorry, if this question has been asked and answered before.
My Game can be compared to "Candy Crush". This is what I have: A 2-dimensional grid (8x8) filled with 64 tiles of 5 different colors. Once you click on one of the tiles, it and all its neighbors that are of the same color are detected, bundled in a List and then deactivated.
This is what I want: Once you click on say a blue tile, an animation is played. The clicked tile and all its neighbors grow in size relative to each other (so without overlapping each other), wiggle and then puff out of existence.
The -puffing out of existence- is not of interest now. I will see if I can draw a few sprites or use a particle effect. However the growing and wiggling animation is what I am concerned about. To achieve this I have researched many different ways but they all seem to be overly convoluted. And I am afraid to do it wrong.
So, do I 1 attach an "Animator component" to all tiles. Create an "Animator Controller" that they all share. Then create 2 states in the controller, one idle and one -GrowAndWiggle- and then script a trigger, which tells the Animators of each of the tiles to transition to and from the -grow and wiggle- state? Alternatively script: foreach (relevant Tile){GetComponent(Animator).Play("GrowAndWiggle") }
(However that means all tiles will be animated throughout the game in an idle state. And once they are clicked they will grow but overlap with each other)
Do I 2 create an IEnumerator that grows the size and moves the position of all relevant tiles once I click on one of them?
Do I 3 manipulate size and position from within the update function while for example a boolean called GrowAndWiggle is true?
What I hope for: Isn't it possible to just create a -GrowAndWiggle- clip and then tell the tiles to run it at the expected moment without having to create an Idle clip? And even more: Wouldn't it be possible to have all relevant tiles be regarded as one gameObject, so that if they grow, they grow relative to each other and don't overlap each other?
I am very sorry for the length of this. I hope for your assistance. Thank you!!!
Answer by UnitedCoders · Feb 01, 2018 at 12:43 PM
Assign animator on all of the grid objects, with idle animation, and OnMouseDown change all the respective grid animation state to grow and wiggle, don't use position variable in the animation.
void OnMouseDown()
{
//for all the respective grid(... )
{
obj[i].GetComponent<Animator>().SetTrigger("GrowAndWiggle");
}
}
and OnMouseDown function, change your respective grid state to Idle
void OnMouseUp()
{
//for all the respective grid(... )
{
obj[i].GetComponent<Animator>().SetTrigger("Idle");
}
}
You can do all this using IEnumerator but this is not the good way to Start IEnumerator again and again on every Click , it will cost more memory. And starting multiple Enumerator may behave differently.
You can do all this in Update function as well if you are good at it. Its better than Enumerator but can not say better than Animator. Anybody who solved my confusion with Update or Animator would really appreciated.
Thank you very much for your reply. I did as you said and it works. Wish you well!