• 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
Question by Mark 6 · Jan 24, 2012 at 03:19 PM · rigidbodyvelocitymagnitudestopped

Detecting When all rigidBodies have stopped moving

HI,

I have several gameObjects on the screen and they are using physics to fall over and stuff. What i want to do is have a script that will run to check all the rigidbodies in the current scene has stopped moving or not. I want it to report back when they have stopped so i can call a level complete function.

I am using c#.

Any advice on this would be good as my current code keeps getting stuck in endless loops.

This is what i have so far.

using UnityEngine; using System.Collections;

public class BlockAmount : MonoBehaviour {

 public int blockAmount;
 public tk2dTextMesh blockText;
 
 public delegate void BlocksAllDoneHandler ( bool congrats );
 public static event BlocksAllDoneHandler AllDone;
 
 // Use this for initialization
 void Start () {
     
     blockAmount = 2;
     
     blockText.text = "" + blockAmount;
     blockText.Commit();
 }
 
 void OnEnable ()
 {
     TouchManager.DestroyBlock += TouchManager_DestroyBlock;    
 }
 
 void TouchManager_DestroyBlock( int  block )
 {
     blockAmount -= block;
     blockText.text = "" + blockAmount;
     blockText.Commit();
     
     if ( blockAmount == 0 && AllDone != null )
     {
         
          CheckObjectsHaveStopped();
     }    
     
 }
 
 void CheckObjectsHaveStopped()
 {
     print("checking... ");
     Rigidbody[] GOS = FindObjectsOfType(typeof(Rigidbody)) as Rigidbody[];
 
         foreach (Rigidbody GO in GOS) 
         {
             StartCoroutine ( CheckMoving( GO ) );
             print(GO.name + " " + GO.velocity.magnitude);
         }
 
     
     //AllDone( true );
 }
 
 IEnumerator CheckMoving ( Rigidbody GO )
 {
     do
     {
         print(GO.name + " " + GO.velocity.magnitude );
     } 
     while (GO.velocity.magnitude > 0.1 );
         
     yield return null;
 }
 
 void Update()
 {
     CheckObjectsHaveStopped();    
 }

}

Comment

People who like this

0 Show 2
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 luizgpa · Jan 24, 2012 at 03:45 PM 1
Share

Why are you starting a coroutine for every Rigidbody every frame?

avatar image Mark 6 · Jan 24, 2012 at 03:48 PM 0
Share

Dunno really seamed the way to do it by making each rigidbody loop untill stopped.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by luizgpa · Jan 24, 2012 at 04:26 PM

If you want a coroutine that will end only when all the rigidbodies have stopped it sould be something like this:

 void Start()
 {
    StartCoroutine(CheckObjectsHaveStopped());
 }
 
 IEnumerator CheckObjectsHaveStopped()
 {
     print("checking... ");
     Rigidbody[] GOS = FindObjectsOfType(typeof(Rigidbody)) as Rigidbody[];
     bool allSleeping = false;
     
     while(!allSleeping)
     {
        allSleeping = true;
 
        foreach (Rigidbody GO in GOS) 
        {
           if(!GO.IsSleeping())
           {
              allSleeping = false;
              yield return null;
              break;
           }
        }
     
     }
     print("All objects sleeping");
     //Do something else
 
 }

Some observations: I'm starting the coroutine in Start, but you could start in another place. Just make sure to start it once. Another point is that I'm checking if the object IsSleeping instead of velocity.magnitude , but you could change it back.

Comment
Thom Denick
aldonaletto
ljsiri
garethdenyer

People who like this

4 Show 3 · 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 markpollard · Jan 24, 2012 at 05:04 PM 0
Share

Thanks that worked a treat.

avatar image markpollard · Jan 24, 2012 at 11:32 PM 0
Share

This is great thanks

avatar image aldonaletto · Jan 24, 2012 at 11:37 PM 0
Share

@Luiz is right: the rigidbody energy is "stored" in velocity and angularVelocity, thus you should check both - and that's exactly what IsSleeping() does.

avatar image

Answer by MIckeasdasdas · Jun 14, 2014 at 02:49 PM

You don't need the while loop and the break. The while loop will end when you do return. foreach (Rigidbody GO in GOS) { if(!GO.IsSleeping()) { allSleeping = false; yield return null;

      }
 }

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

increase float when another one decreases 1 Answer

Rigidbody magnitude 3 Answers

checking angular magnitude 1 Answer

Rigidbody.velocity.magnitude only returns value of the Y axis 1 Answer

How to apply relative force to Ball using another object? 0 Answers


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