- Home /
Preventing rigidbody sleeping altogether
As the title says, is there any way to completely prevent a rigidbody from sleeping, without having to add 0 force or set their position to their position? Setting the sleep velocity/angular velocity to 0 doesn't prevent it, once they actually stop moving they go to sleep.
In my scenario I am dropping salt piles to slow & kill slugs that collide with individual salt particles. I use OnTriggerStay to determine if they are touching one and it works, but as soon as the salt particles come to a rest and sleep they stop detecting collisions.
I've looked up a few other similar questions and they all seem to have solved it by doing stuff like this:
transform.position = transform.position;
transform.Translate(0,0,0);
rigidbody.AddForce(Vector3.zero);
There will potentially be hundreds of salt particles on the screen at a time, I don't want to have to throw them into a list and loop through it every frame just to set their position to where it already is or add no force, then remove them when they're dissolved. The slugs aren't using rigidbodies, I didn't want them able to push the salt around though I may just end up doing that if there's no way to prevent sleeping without executing some code every frame.
If you go to:
Edit > Project Settings > Physics
...and set Sleep Velocity to 0.0 or you set the velocity directly:
rigidbody.sleepVelocity = 0.0;
Does this solve the problem?
No, once they stop completely they seem to be put to sleep again and OnTriggerStay stops being called. I ended up parenting them all to an object and using GetComponentsInChildren to call WakeUp() on all the rigidbodies, it's not much of a problem but it'd be nice to be able to disable it.
Answer by plasticYoda · Jun 07, 2015 at 03:27 AM
I ended up with this:
void Update () {
if (rigidbody.IsSleeping())
rigidbody.WakeUp();
}
My player object doesnt move - the scenery moves towards it, so it would fall asleep pretty quickly and didnt collide with anything. I only had a rigidbody on the player, not the objects, and a sleeping player was the problem!
Answer by NathanJSmith · Aug 20, 2019 at 09:38 AM
You can set Rigidbody.sleepThreshold to 0 to prevent rigidbody sleeping.
m_Rigidbody = GetComponent<Rigidbody>();
m_Rigidbody.sleepThreshold = 0.0f;
Answer by Fergicide · Apr 09, 2020 at 12:25 PM
I believe "rigidbody.WakeUp();" is the best solution, but I think it should go in FixedUpdate, with perhaps [DefaultExecutionOrder(-100)] to have it run before other FixedUpdate instances for that rb.
Better to solve the issue on an individual basis than reduce Rigidbody.sleepThreshold globally and incur all of that physics performance hit.
My timer testing suggests WakeUp is much faster than "thisTransform.position = thisTransform.position" (cached transform).
Answer by amitDklein · Sep 14, 2020 at 05:22 PM
You can use
void Start()
{
this.GetComponent<Rigidbody>().sleepThreshold = 0.0f;
}
this worked for me