Search Unity

Question Two colliders on same position, overlapping confusion

Discussion in 'Physics' started by WirealTalha, Sep 19, 2020.

  1. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    Im creating a simple 2d game In untiy.

    E.g Snake(Box Environment)


    here's the image

    https://ibb.co/Ytss5sS




    These are all 5 colliders. All the borders have a colliders on them to detect if the Player(P) collided with the border.

    X is Home. it also has a collider on it. Now How to detect when the player enters X, It shouldnt die but it should be safe, any other collision (with the borders) should kill the player but not Collision with X. I know how to use OnTrigger functions..

    but the issue is that both X's Collider and RIght Borders Collider are overlapping.(more like Superimposed)

    how to resolve this issue? Kindly help me out.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,420
    They're overlapping because you placed them there so what is the actual issue itself i.e. what is a getting in your way.
     
    WirealTalha likes this.
  3. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    HI,

    The Issue is that I don't know how to handle/Separate these two colliders through script.
    I want that
    When my Player collides with the Border It should Destroy itself (okay, I can do that, No issue).
    When the player Enters/Collides with the Home Collider(Which will always be on the border) it should start next implementation.

    What I am doing right now is checking the collision.collider.name in OnTriggerEnter2D(Collider2d collision )and if its border then destroy the object and if the collider is home then safely start next implementation.

    Right now as the Home and Border are both on the same Position, My player gets destroyed when it enters that Position and Can not start the Home implementations that it should as it has also reached Home aswell.

    This is the issue I am facing.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,420
    I think I follow you and this sounds like an order issue i.e. the player overlaps the border (destroy) and home (next) at the same time.

    The order of callbacks cannot be controlled unfortunately so I would suggest you defer the actual action until all the callbacks have happened i.e. store some state based upon the callbacks then action it later.

    Alternately you can check during the Border callback if you're touching a Home layer using IsTouchingLayer. If you are then you don't want to destroy as you know you'll get a callback on the Home layer. The "IsTouching" looks at existing contacts only so is pretty quick.

    So in short, either store as flags what happened then process it later using your logic on which takes priority or just insert the check to see if you're touching the other layer to give you your priority.

    Hope that makes sense.
     
    Last edited: Sep 21, 2020
    WirealTalha likes this.
  5. WirealTalha

    WirealTalha

    Joined:
    Jul 19, 2018
    Posts:
    142
    Thankyou for your response. Yes you are right. the player overlaps the border (destroy) and home (next) at the same time.

    Okay. I'll implement what you have said and post here the result.
    Thanks once again. I'll check the .IsTouching aswell.