Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnCollisionStay doesn't get called

Discussion in 'Scripting' started by felixk, Jan 2, 2009.

  1. felixk

    felixk

    Joined:
    Dec 17, 2008
    Posts:
    38
    Hi,

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

    I've added a script to the platform:
    Code (csharp):
    1. function OnCollisionStay (collisionInfo : Collision) {
    2. Debug.Log("test");
    3.    
    4. }
    5.  
    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]
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    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.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This is OnCollisionStay, not OnCollisionEnter. Anyway, after an object falls asleep, it won't generate OnCollisionStay messages anymore, so you can do

    Code (csharp):
    1. if (rigidbody.IsSleeping()) {
    2.     rigidbody.WakeUp();
    3. }
    or something.

    --Eric
     
    Marrt likes this.
  4. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    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...
     
  5. felixk

    felixk

    Joined:
    Dec 17, 2008
    Posts:
    38
    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
     
  6. felixk

    felixk

    Joined:
    Dec 17, 2008
    Posts:
    38
    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."
     
    kbth likes this.
  7. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    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! :/
     
    kbth likes this.
  8. Fox-Handler

    Fox-Handler

    Joined:
    May 2, 2014
    Posts:
    74
    I'm pretty sure it doesn't sleep. It actually should get called continuously while in contact with an object.
     
  9. tgouala-wellfiredLtd

    tgouala-wellfiredLtd

    Joined:
    Jun 8, 2013
    Posts:
    99
    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.
     
  10. Noxury

    Noxury

    Joined:
    Mar 8, 2015
    Posts:
    22
    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)?
     
  11. NoScopingDude

    NoScopingDude

    Joined:
    Apr 7, 2016
    Posts:
    7
    Csharp has a oncollisionstay void function you can use.
     
  12. kbth

    kbth

    Joined:
    Apr 19, 2017
    Posts:
    2
    There's a line at the end now...... easily missed... so make that over 7 years?