Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Collisions not Working 2

Discussion in 'Scripting' started by Eisenwelle, Apr 8, 2021.

  1. Eisenwelle

    Eisenwelle

    Joined:
    Mar 18, 2021
    Posts:
    22
    This thread is an extension of the last thread I posted: https://forum.unity.com/threads/collisions-not-working-properly.1089928/

    My bool isGrounded is set to true when my player collides with an object on the ground layer, and set to false when that collision ends. Here is my script for this:
    Code (CSharp):
    1. void OnCollisionEnter(Collision coll)
    2.     {
    3.         if (coll.collider.gameObject.layer == LayerMask.NameToLayer("Ground"))
    4.         {
    5.             isGrounded = true;
    6.         }
    7.     }
    8.  
    9. void OnCollisionExit(Collision coll)
    10.     {
    11.         isGrounded = false;
    12.     }
    Here's the problem. When I'm already in contact with a ground object (we'll call this G1), and I collide with a different ground object (G2), things mess up. When I end my collision with G2, isGrounded is set to false, despite me still being on contact with G1. If I can somehow keep track of multiple collisions at once, I think that might fix my problem, but my beginner brain has no clue how to do that. Any help figuring this out is appreciated!
     
  2. Eisenwelle

    Eisenwelle

    Joined:
    Mar 18, 2021
    Posts:
    22
    Also, should I specify in OnCollisionExit that I only want isGrounded to be set to false if the collider was a ground layer object, or is that unnecessary?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    The most obvious change is to do the check in line 3 rather than unconditionally setting isgrounded false.

    Another approach is to always set isgrounded to false an use OnCollisionStay() instead
     
  4. Eisenwelle

    Eisenwelle

    Joined:
    Mar 18, 2021
    Posts:
    22
    I used OnCollisionStay() instead, and that solved my problem. Now that I have that, do I still need OnCollisionExit(), or does OnCollisionStay() account for that?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    OnCollisionStay will be called every frame you are in contact with another collider. So you'd use that to set isGrounded to true. But there is no opposite method that is called when you're not in contact, where you'd set isGrounded to false. So you probably would want to use OnCollisionExit for that.

    Note that depending on the order they called, you may get occasional frames where you're in contact but isGrounded is false, but the situation is corrected when OnCollisionStay is called again. For example, you leave G1 for G2. OnCollisionStay gets called for contact with G2, then OnCollisionExit is called for exit G1 (I don't know if they are called in that order, just an example), for that frame isGrounded would be false when you are really in contact. But the next time OnCollisionStay is called then isGrounded is back again.
     
    Kurt-Dekker likes this.