- Home /
Check if multiple gameObjects play animation
Could someone point me to a direction or suggest the best or simplest way to check if any gameobject's animation is playing? There seems to be some different alternatives (arrays, animator control), not sure which way to go since I'm still unfamiliar with animations in Unity. I just want to let the animation of some gameObjects be done before next command execute.
Like this, I have an array of cubes that move when they are clicked (using Raycast). When they are clicked, it should not be possible to click on the next one until animation is done. (Animation is about 1 second.)
Now, the Raycast script says; if MouseButtonDown, move the hit.collider and play the collider's animation. That all works fine.
{ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown(0))
{
hit.collider.gameObject.animation.Play();
hit.collider.gameObject.transform blabla;
}
Problem now, if you click fast you can interfere as it is now and continue to click on several cubes, which should not be possible.
I would like something like this Before the above:
if (!animation.isPlaying) Do the stuff.
But how define this on multiple gameObjects? The script is on an empty gameobject, and my instinct say it should be so, cause if each cube had the script they would not know if any other cube was playing any animation. My hunch is also that the script won't know of any object outside the moment it's hit by the raycast.
Answer by Dakwamine · Mar 14, 2014 at 05:37 PM
Using a single manager is a good idea.
Basically, your question is: "how can I make the script wait while the animation is playing?".
I would suggest you to use Coroutines to make this kind of stuff: http://docs.unity3d.com/Documentation/Manual/Coroutines.html
void Start()
{
StartCoroutine(Scenario());
}
IEnumerator Scenario()
{
while(true)
{
GameObject clickedCube;
// Wait for click on object
while(true)
{
while(!Input.GetMouseButtonDown(0))
yield return null;
// If cursor is not on cube, restart loop
Ray ray = /* your ray */
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
clickedCube = hit.gameObject;
else
{
yield return null; // Wait for next frame to prevent infinite loop
continue;
}
while(!Input.GetMouseButtonUp(0))
yield return null;
// if cursor is on the same cube, it's ok!
ray = /* Update your ray here */
if(Physics.Raycast(ray, out hit))
if(clickedCube == hit.gameObject)
break;
}
// Play your animation
clickedCube.animation.Play();
// Wait animation end
while(clickedCube.animtion.isPlaying)
yield return null;
}
}
I'm not sure about the syntax. Hope this helps anyway.
Thanks! Yes, I guess that would be a way, I'm gonna try that later, I'm not used to coroutines yet, just know it can make a mess under update so I have avoided them... I'm trying Another solution now, just a bool statement together with a for loop. I'll be back if I get it working.
Answer by NickMx · Mar 15, 2014 at 01:04 AM
So, guys, everything's a piece of cake when you know how to do it... Dakwamine's solution can be a better choise for some occasions. This is my solution and it's quite simple:
Make a function first, (if you don't, you end up doing quite the opposite, like I did first, and be able to click ONLY when animation is played :) )
void MyMouseblock()
{ for (int goLength = 0; goLength < Cubes.Length; goLength++)
if (Cubes[goLength].animation.isPlaying )
{
AnimOn = true;
Debug.Log ("Anim On is true");
}
else
{
AnimOn = false;
Debug.Log ("Anim On is false");
}
}
then put it under Update with the Raycast and Input of Mouse commands...
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown(0))
{
if (AnimOn == false)
{
Do the stuff;
}
This works for my particular setup at the moment (having short animations and other stuff), but you may be observant on that it can be problematic in some cases, since the input of mouse also, in my case should trigger the animations, but first check that no other animations were playing. I still don't know if some of it should be better of in LateUpdate or Update.