- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Gantavya · Sep 01, 2014 at 11:07 PM ·
javascriptrigidbodysleeping
All objects sleeping not working
I have the following JavaScript to detect when all objects stopped moving and show the menu. But this code does not seem to work as menu never shows up. Can you help me to fix the code?
#pragma strict
static var playing;
var allSleeping: boolean = false;
function Start () {
yield CheckObjectsHaveStopped();
}
function CheckObjectsHaveStopped()
{
print("checking... ");
var GOS: Rigidbody[] = FindObjectsOfType(Rigidbody) as Rigidbody[];
allSleeping = false;
while(!allSleeping)
{
allSleeping = true;
for (var GO: Rigidbody in GOS)
{
//if(!GO.IsSleeping()) //Keeps printing "All sleeping is False... "
if(GO.velocity.magnitude <= 0.1f) //Prints "All sleeping is False... ", even after all objects stopped moving
//Menu never shows up
{
allSleeping = false;
print("All sleeping is False... ");
yield WaitForSeconds(2);
break;
}
}
}
print("All objects sleeping");
}
//Show the menu only when all objects stop moving
function OnGUI() {
if (allSleeping == true) {
GUI.Box(Rect (Screen.width/8, Screen.height/4, Screen.width/1.3, Screen.height/2), "Game Name");
if (GUI.Button(Rect (Screen.width/2.6, Screen.height/2.8, Screen.width/4, Screen.height/10), "Play")) {
playing = true;
}
if (GUI.Button(Rect (Screen.width/2.6, Screen.height/1.7, Screen.width/4, Screen.height/10), "Quit")) {
Application.Quit();
}
}
}
Comment
Answer by SnotE101 · Sep 02, 2014 at 01:33 AM
I would try a different approach.
Try to save the transformation data in a Vector3 variable. Then start a loop, so every 5 or so seconds it updates and compares the last updates transformation data with the updated one. If they are the same then you know the objects have stopped.
Good luck!
Your answer
Follow this Question
Related Questions
Collsion without ririgibody 1 Answer
Using "Tag" to disable Rigidbody Constraints 1 Answer
Make a object move forwards 1 Answer
Adding force to rigidbody 1 Answer
Enemy death help 1 Answer