rigidbody IsSleeping() Problem

Hey guys, here’s a very strange problem about rigidbody,

This code works fine, While the ball is still rolling, it logs False, when the ball stopped, it logs True

    void FixedUpdate()
    {
        if (shouldCheckSleep)
        {   
                Debug.Log(targetBall.rigidbody.IsSleeping());
        }
    }

However, I added an IF statement ,wanted to do something when the ball stopped, it won’t work anymore, it just keeps logging True, even when the ball is still moving!

    void FixedUpdate()
    {
        if (shouldCheckSleep)
        {   
           if (targetBall.rigidbody.IsSleeping())
            {
                Debug.Log(targetBall.rigidbody.IsSleeping());
           }
        }
    }

Any one knows why?

1 Like

Are you sure it hasn’t just stopped logging? The way that code should flow, is that it would log “true” every update until the ball started moving and then it would stop logging altogether. The previous logs would remain, though, so if you were viewing the one line console it would appear to “continue” logging. You must’ve done something else. Is the sleep velocity the same? Is the test scene the same?

Thank you Metabble! You got the key to the problem! I tested it again, Here’s the flow:

  1. The “shouldCheckSleep” = False ,at the beginning, So it won’t check when we haven’t shot the ball;
  2. I Shot the ball, and some other code set “shouldCheckSleep” = True right away;
  3. Just like what you said, it DIDN’T KEEP LOGGING true, instead, IT JUST LOGGED True once , and then stopped (which I didn’t notice before), when the ball finally stopped, it started to log True per frame;

With your help, now I found the problem, it’s the strange one frame of “True” right after I shot the ball.
So I am wondering , if it needs one frame to change the sleeping state of a rigidbody.

I’m not sure if all rigidbodies are sleeping by default or not. It might have to do with the order of the calls. If the debug log runs before you add any forces to the ball, it’d be below the angular and linear sleep velocities, so that might be the cause. It’s an interesting question, but sadly not one I can answer definitively.