Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NickMx · Mar 14, 2014 at 05:01 PM · animationgameobjectarraysif-statementsisplaying

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NickMx · Mar 14, 2014 at 07:50 PM 0
Share

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.

avatar image
0

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;
                 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NickMx · Mar 15, 2014 at 11:57 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Keyframes of animated Child gameobject not shown by parent gameobjects animator issue ? 0 Answers

Best way to - Animation Sync - Multiple gameObjects. 0 Answers

Find one inactive player (gameobject) 2 Answers

Can I make animations snap to a frame? 1 Answer

Best way to animate a guy putting on a hat 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges