Search Unity

ContactPoint2D when dealing with triggers (Collider2D)

Discussion in 'Scripting' started by deLord, Mar 18, 2019.

  1. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    257
    Hello

    I have an issue when I want to code that my player falls into an abyss when his feet (a BoxCollider2D) touches the abyss' PolygonCollider2D (isTrigger).
    My player's rigidbody is Dynamic, he has 2 isTrigger Colliders of which only 1 should trigger (the feet), the abyss prefab has no Rigidbody2D and has a PolygonCollider2D.

    Abyss and Colliders.png

    I understand that I can get the ContactPoint2D[] when I add a (static) Rigidbody to my abyss and uncheck the isTrigger box in the PolygonCollider but this is not what I want because then I created a collider with which the other Collider of the player (CircleCollider2D) collides, thus blocking the player from falling into the abyss if he enters in the "right" angle.

    The problem is that I tried to code the falling direction but since the PolygonCollider can have it's center anywhere, I cannot get a Vector2 which is just (abyss.transform.pos - plr.transform.pos). The abyss can have any shape.
    The only possibility I see is to get the ContactPoint2D, because plr.rigidbody.velocity doesn't work either (if you run perpendicular to the abyss' collider).

    So how can I resolve this issue and drag my player into the abyss (not the middle, just in the direction of the contact point)?

    Thank you
     
  2. Luxgile

    Luxgile

    Joined:
    Nov 27, 2016
    Posts:
    16
    I don't know the purpouse of the box collider, but probably the best way to do this would be a single collider without trigger for the player and a collider with trigger in the abyss.

    What else you can do is leave the abyss with no trigger and, in the moment it collides just enable the trigger or disable the collider (Although that's not actually a good practice and probably will give you problems in the future)
     
  3. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    257
    The purpose is that some tiles react to the player's feet and others react to his body.
    And even if I had just one collider, how would that solve my problem? Without a ContactPoint I can not make a proper calculation
     
unityunity