I have been working on this pool game for about a month now. I have rigidbodies attached to balls with spherecollider and my board has mesh collider. every thing was going well until i started working with gameplay logic (fouls, ball potting etc…) when i came across this problem with triggering the events that checks my game play logics. I need to check for fouls when the balls have come to rest and i tried doing this on update function as well as fixed update but i could not figure out when exactly are these function being called. some times it works just fine but most of the time these events get called even when balls are moving.
\
`void FixedUpdate()
{
if(currentPlayer.justShot)
{
StartCoroutine(CheckObjectsHaveStopped());
if(ballsCameToRest)
{
actionsOnBallRest();
}
}
}
IEnumerator CheckObjectsHaveStopped()
{
Rigidbody[] balls=FindObjectsOfType(typeof(Rigidbody)) as Rigidbody[];
bool allSleeping=false;
while(!allSleeping)
{
allSleeping = true;
foreach (Rigidbody GO in balls)
{
if(!GO.IsSleeping())
{
allSleeping = false;
yield return null;
break;
}
}
}
if(allSleeping)
{
ballsCameToRest=true;
}
}
public void actionsOnBallRest()
{
currentPlayer.ShotCount++;
currentPlayer.justShot=false;
if(!didPot)
{
switchPlayer();
}
checkforFoul();
}`
I am planning to put all my game play logic inside the function actionsOnBallRest() which should be called only when all the balls come to rest (sleep) after some force has been added to them. I have been looking for the solution for few days now but haven’t got much of success.
If anyone cud help me out with this I’d be very very grateful.