Search Unity

Multiple and different Box Collider 2D-Objects

Discussion in '2D' started by nccbk, Jan 4, 2021.

  1. nccbk

    nccbk

    Joined:
    Jan 4, 2021
    Posts:
    1
    Hey there,

    I am a newbee in unity and fighting with my first collisions right now...

    I am building a 2D race game on a highway. The player can control the movement of the car in the lane in the vertical direction (accelerate/decelerate). Infront of the players car and also behind I placed another car, both equipped with a Box Collider 2D (Trigger enabled) and also a Rigidbody 2D. The car of the player also features these two objects and detects any collision with:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision)
    So this works fine and detects the crash...

    Now the player should keep his/her car inside of the green box in between the other cars and score points with that.
    My idea was to use a normal spite for the box and also the Box Collider 2D and also a Rigidbody 2D.

    But how can I make shure I detect if my car is fully inside of the green box and don't trigger the other function shown above that would end the game?

    I already tried https://answers.unity.com/questions/1417989/how-can-i-detect-multiple-triggers-on-same-gameobj.html
    but at least the first answer does not work for me.

    Thanks!