I am developing an 8 ball pool/billiards game in which I want to find out when all the balls on the table stop.
I tried using this piece of code however the number of balls that have stopped keeps on incrementing to massive numbers when the value should be 16 or less.
IEnumerator CheckObjectsHaveStopped()
{
bool allSleeping = false;
Rigidbody[] GOS = FindObjectsOfType(typeof(Rigidbody)) as Rigidbody[];
while (!allSleeping)
{
allSleeping = true;
foreach (Rigidbody GO in GOS)
{
if (GO.velocity.magnitude <= 0.1)
{
Balls_Stopped += 1;
Debug.Log("Balls Stopped = " + Balls_Stopped);
yield return null;
}
}
}
if (Balls_Stopped == Balls_Left)
{
print("All objects sleeping");
//Do something here
}
}