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 j46r0 · Aug 22, 2014 at 11:03 AM · c#gameobjectrigidbody

Why does IsSleeping() keep returning false? (Billiard Logic)

I am creating a Billiard game and I have each ball as a GameObject on the table. I have added a c# script which acts as controller and gamelogic script.

 public Camera cueCam;
 private bool pauseGame = false;
 private bool areStill = false;
 
 public GameObject[] Full_balls;
 public GameObject[] Striped_balls;
 
 public GameObject ball8;
 public GameObject ballCue;
 
 private int activeBalls = 14;
 
 void Update () {
         if (Input.GetKey(KeyCode.Escape))
             pauseGame = !pauseGame;

         areStill = checkMovement();
     }
 
 

On each OnGUI call, I am checking if cueBall camera is enabled, if there is any movement and if game is not paused. If all three condition are true, game interface comes up and player can control the shot.

 void OnGUI() {
         if (cueCam.enabled && areStill && !pauseGame)
 
 .
 .
 .


Now here is the problem. Function checkMovement() is returning false, because each ball I check movement for with IsSleeping() returns false.

 private bool checkMovement()
     {
         bool no_movement = false;
 
         int counter = 0;
         foreach (GameObject ball in Full_balls)
         {
             if (ball.rigidbody.IsSleeping())
                 counter++;
             else
                 break;
         }
 
         foreach (GameObject ball in Striped_balls)
         {
             if (ball.rigidbody.IsSleeping())
                 counter++;
             else
                 break;
         }
 
         if (GameObject.Find("Ball8").rigidbody.IsSleeping())
             counter++;
 
         if (ballCue.rigidbody.IsSleeping())
             counter++;
 
         if (counter == activeBalls) 
         {
             checkPots();
             no_movement = true;
         }
 
         return no_movement;
     }

I just cannot figure out where the problem lies. The code seems fine to me. Is there a tweak I can do with GameObject properties? Please help out!

Comment
Add comment · Show 4
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 KiraSensei · Aug 22, 2014 at 12:15 PM 0
Share

I'm not very familiar with sleeping modes, but maybe it is because you test it in OnGUI function (called several times per frame). Try to valuate a global boolean from the Update method like :

 private boolean is$$anonymous$$oving = false;
  
 void Update () {
         is$$anonymous$$oving = check$$anonymous$$ovement();
 }

And then test this variable in the OnGUI function :

 void OnGUI() {
     if (cueCam.enabled && is$$anonymous$$oving && !pauseGame) {
         ...
     }
 }

avatar image j46r0 · Aug 22, 2014 at 01:49 PM 0
Share

Thank you for the tip. Edited the code, but it still does not register as balls being still.

  • for better code logic!

avatar image KiraSensei · Aug 22, 2014 at 02:59 PM 0
Share

Did you try to go through your check$$anonymous$$ovement method with a debugger ? Just to be sure where no_movement is valuated to false and understand why ?

avatar image j46r0 · Aug 22, 2014 at 04:30 PM 0
Share

Of course I went through with a debugger. The thing is, the function .isSleeping() never returns true. So my counter never reaches the number of active balls.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

C# Preserving GameObjects' Previous Meshes 1 Answer

C# Gameobject Rigidbody Mouse Collision 1 Answer

Player instantiates Backwords 0 Answers

C# RotateAround for 2DRigidBodies 1 Answer

How to determine the direction of an object to the player. 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