- Home /
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!
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) {
...
}
}
Thank you for the tip. Edited the code, but it still does not register as balls being still.
for better code logic!
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 ?
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.