Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Detect Collision Up and Down

Discussion in '2D' started by shohagmist, Apr 17, 2016.

  1. shohagmist

    shohagmist

    Joined:
    May 19, 2015
    Posts:
    37
    I have a object in the middle of scene (i.e. (0,0) point) with a Box Collider 2D. Small objects are coming towards it from Up (+y axis) and Down(-y axis). Those small objects also have Box Collider 2D and Rigidbody2D attached with them.

    I can detect collision, my code is working with OnCollisionEnter2D() function. Everything is okay till now.

    but my problem is, when a small object is coming from Down (-y axis) and hitting the Center Object, collision is detected after some while of colliding. then I used OnCollisionExit2D(), still it is giving me the same result. But, the small object coming from Up(+y axis), detecting the collision as soon as it is hitting the center object.

    Why is this happening? Which direction is the Collision Enter of a Box Collider?
     
  2. shohagmist

    shohagmist

    Joined:
    May 19, 2015
    Posts:
    37
    It seems that the small objects with RigidBody2D had gravity. In constraints, I freezed the position Y and that disabled the Gravity. Now it is working. OnCollisionEnter2D() function is detecting collision from both Up and Down, as soon as it hits the center Object.

    Though I didn't find any proper reason of the earlier problem. If anyone reading this, it will be a big help if you give any idea.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    Not sure what you mean by "some while of colliding" but if you're using discrete collision detection then you won't get a callback until the objects are overlapping. Box2D then solves the overlap over time with impulses (it doesn't solve it instantly) but you can control how 'fast' with the Baumgarte scale (one is for discrete the other for continuous).

    If you want to stop overlaps but at the expense of performance, you can use Continuous collision detection-mode on the moving bodies.

    All this assumes of course that these bodies are moving using their own velocity and you're not simply updating their positions explicitly.