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 Jan_Julius · Nov 03, 2014 at 09:49 AM · coroutinebooleandelaycondition

Delay something until no object has a certain condition

I don't want the game to advance a level if an object is falling , I already have something on each object that detects this an "isFalling" boolean it goes to false if it doesnt fall simple enough but what would be a good way of checking if any of them is falling, they are all the same objects but what would be a non-expensive way to check wether one is falling and then I want to delay the execution of other code as in make it wait till nothing is falling anymore what would be a good way of doing this?

Thanks,

Jan Julius.

Comment
Add comment · Show 3
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 Itaros · Nov 03, 2014 at 09:52 AM 0
Share

Are they instantiated at runtime or they was defined in scene?

avatar image richyrich · Nov 03, 2014 at 10:05 AM 0
Share

Every time you have an object that starts falling, increment a static int. Each time an object stops falling, decrement that same static int. When the static int gets to zero, you know the level can advance.

avatar image Jan_Julius · Nov 03, 2014 at 10:29 AM 0
Share

They are instantiated and richyrich I was thinking about this but the objects can get destroyed and new ones instantiate, they can also fall when other ones get destroyed and the level can't end whenever one is falling.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by fafase · Nov 03, 2014 at 10:03 AM

Keep track of all rigidbodies in your game.

 private List<Rigidbody>rigList = new List<Rigidbody>();
 void Start()
 {
     var rig = FindObjectsOfType<Rigidbody>();
     foreach(Rigidbody r in rig)
     {
         rigList.Add(r);
     }
 }

Now you got all rigidbody at start.

Any time you add a new rigidbody to the game add it to the list, and also remove it when you destroy one.

Finally, when you should be transiting, check if the velocity is 0:

 bool CheckMovingRigidbody()
 {
     for(int i = rigList.Count - 1; i >=0; i--)
     {
         if(rigList[i].velocity.magnitude < 0.01f)
         {
             rigList.RemoveAt(i);
         }
     }
     if(rigList.Count == 0)
     {
        return true;
     }
     return false;
 }

You use this method to check if all rigidbodies are not moving, if the movement is small enough, you remove the item from the list and you try again later.

Note that this considers that once a rigidbody is not moving, it won't get moving again since it gets removed from the list and won't get back in.

EDIT: since you say you have all objects to fall until they are told not to apply the same process but instead of getting the rigidbody and checking the velocity, you grab the script where they are told to move and set a boolean there and set it to false when not moving:

 public class Movement:MonoBehaviour{
     private bool isMoving = false;
     
     public void MoveObject(){
         // Some code
         isMoving = true;
     }
     public void StopMoving(){
         isMoving = false;
     }
 }

 











Comment
Add comment · 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 Jan_Julius · Nov 03, 2014 at 10:22 AM 0
Share

I am not using rigidbodies for this though, the objects are just constantly transfor$$anonymous$$g down until my script says they can't anymore.

avatar image Itaros · Nov 03, 2014 at 10:34 AM 0
Share

Then adapt it. This answer is awesome

avatar image Jan_Julius · Nov 03, 2014 at 11:58 AM 0
Share

Does a rigidbody apply velocity when it's being transformed and gravity is turned off?

avatar image
1

Answer by SteveBC · Nov 03, 2014 at 11:16 AM

Try saving them all in an array or list of your objects (make it an array/list of the Class with the isFalling variable to avoid unnecessary GetComponents and check for the value of isFalling of each one in a for-loop.

 YourClass[] objects = new YourClass[5];

 for(int i=0; i<objects.Length; i++) {
   if(objects[i].isFalling) {
      //Do fun stuff
   }
 }
Comment
Add comment · Show 2 · 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 fafase · Nov 03, 2014 at 11:27 AM 0
Share

Please read other answers first. I understand you want to help but you do not provide more info that I did.

avatar image AlucardJay · Nov 03, 2014 at 11:33 AM 0
Share

Actually, looking at the timestamp on both answers, this one was first. It was stuck in the moderation queue. Just treat as a simultaneous post. Upvotes for everybody.

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

make events occur only at specific times 1 Answer

Better way to delay a function for a few seconds? Javascript 1 Answer

strange behaviour for coroutine and invoke 1 Answer

Inserting a delay 2 Answers

Executing an Action after a Coroutine has finished 2 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