- Home /
Use Of OnCollisionStay()?
So i have been studying triggers and colliders recently and how they are used but i don't seem to understand the use of OnCollisionStay? I understood how and why OnTriggerStay is needed but couldn't understand why OnCollisionStay is needed or even used. Can someone explain it with an example?
OnCollisionStay should really be avoided unless incredibly necessary. The per-frame processing will add massive load to the cpu. Intensive!
Answer by alelavoie · Jan 17, 2018 at 03:43 AM
It is called as long as the two bodies are colliding.
OnCollisionEnter -> OnCollisionStay - OnCollisionStay - ... --> OnCollisionExit.
For example, imagine you are working on a helicopter game and you want the helicopter's engine to run at 80% of it's maximum strength when the skids are touching the ground.
void OnCollisionStay(Collision collisionInfo) {
if (collisionInfo.gameObject.name == "Ground") {
//Reduce motor strength to 80%
}
}
Answer by sparkzbarca · Jan 17, 2018 at 03:43 AM
OnCollisionStay is used to do something while a collider is well colliding.
it's used for two objects continue to touch for a period basically.
ontriggerstay (which is basically to check an area) and on collision stay (which is used when two objects continue to touch) could be used in these two ways.
if you imagine a room with for example a fire pit, when your player walks in the fire pit you take damage, as long as you continue to stay in the flames, you keep taking damage.
for collision, if a wall was electrified, you continue to take damage while touching.
if you have something like a puzzle game where you push blocks around, on collision enter could be you do the animation to get in position to push, but on collision stay could be you push the block.
this would be useful for some kind of tomb raider/zelda puzzle where you need to push blocks around to jump on them to access areas or to place them on floor triggers to hold them down.
in gneral it's useful if you want something to happen but ONLY while two objects are directly touching.
Be that applying damage for touchign something, or pushing/pulling or even something like (i'm not saying htis is ideal)
on collision stay
if collider == terrain isgrounded = true;
you could use the fact that your touching the ground to keep updated on the fact that your grounded.
Your answer
Follow this Question
Related Questions
New physics creating a problem in working game. 0 Answers
Performance issues with trigger colliders and lots of projectiles. 2 Answers
OnTriggerEnter2D triggering multiple times when moving around inside the trigger area. 1 Answer
Curious about "Vortex" force (HELP) 2 Answers
Pulling boxes 0 Answers