Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted 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:
    13,753
    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:
    10,809
    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.
unityunity