OnCollisionStay doesn't get called

Hi,

I’m trying to detect when a box falls on a platform and stops moving.

I’ve added a script to the platform:

function OnCollisionStay (collisionInfo : Collision) {
Debug.Log("test");
	
}

If the box lands on the platform, the debug outputs “test” but it stops!

The docs say: “OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.”

So I expect “test” debug output to be output non-stop but it does stop. Anyone have any ideas why this is happening.

Thanks,
Felix[/code]

Perhaps the docs are wrong? I would think that a collision is when two volumes intersect, not when they ‘touch’, and the physics engine resolved the collision after the ball landed on the box.

Just a guess though and I might be wrong, see if OnCollisionExit() is being fired to verify.

This is OnCollisionStay, not OnCollisionEnter. Anyway, after an object falls asleep, it won’t generate OnCollisionStay messages anymore, so you can do

if (rigidbody.IsSleeping()) {
	rigidbody.WakeUp();
}

or something.

–Eric

1 Like

Two things:

  • Make sure Collapse isn’t set in the console window. This will prevent you from seeing the debug log more than once.

  • I’m not sure offhand what happens when rigidbodies sleep, with OnCollisionStay. If you’re seeing a second or so of “test” messages, and then it stops, your Rigidbody is sleeping.

But, yes, OnCollisionStay is called once every frame as long as a Rigidbody is in contact with something…

Looks like the only way i can get OnCollisionStay to get fired continuously is by waking up the Rigidbody with .WakeUp

It’s not the ideal solution as I’m spending CPU cycles on calculating static values, but it works.

Thanks for the help.

Felix

If anyone from Unity is reading this, the docs should be changed to:

“OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider as long as that rigidbody isn’t asleep.

1 Like

More than two years and the docs haven’t yet been fixed? I just spent one hour trying to understand why my method wasn’t called, and I finally realized onCollisionStay is fired only if a rigidBody isn’t asleep. Grrrrr! :confused:

1 Like

I’m pretty sure it doesn’t sleep. It actually should get called continuously while in contact with an object.

In my case OnCollisionStay is called but only if the script is attached to the rigidbody of my parent object, not when it is attached to one of the subcolliders. The problem is that I would like to get this function called on a gameobject holding one of the subcolliders component to know which collider actually collided.

Just be be clear of its timestep: Is onCollisionStay() using the fixed timestep, like it gets called everytime right after fixedupdate (default timestep 0.02)?

Csharp has a oncollisionstay void function you can use.

There’s a line at the end now… easily missed… so make that over 7 years?