Search Unity

One more OnCollisionEnter2D problem..

Discussion in '2D' started by Leggy7, Apr 8, 2015.

  1. Leggy7

    Leggy7

    Joined:
    Feb 18, 2014
    Posts:
    21
    I came across the most of the threads regarding this problem, quite common..

    My scenario is as follows:

    2 objects: wall and arrow.

    They seem to be unable to interact.
    They lay on 2 different layers, which are flagged in the Layer Collision Matrix so they can interact.

    They are both in the same xy plane (same z coord)

    Both objects have PolygonCollider2D and none have Rigidbody2D.

    The arrow is trigger and the wall is not (btw I tried every combination).

    OnCollisionEnter2D just has a Debug.Log("colliding") and it never gets called.


    Then, what I noticed is that another object (Player) collides with wall but not with the arrow.


    I don't know what to try.. I'm using unity 4.5 (but I tried also on 4.6) and no more ideas come to my mind.

    Any suggestion?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    If you don't add a Rigidbody2D then the colliders are static and should never ever be moved at runtime. This is why static/static will never collide. If you want to move them then add a Rigidbody2D. If you intend to move them using the Transform then set the Rigidbody2D to be Kinematic (not dynamic).
     
  3. Leggy7

    Leggy7

    Joined:
    Feb 18, 2014
    Posts:
    21
    Thank you for your reply.

    Actually if I pay attention to the scene view I see the arrow collider moving with its gameobject so I don't think this is the point.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    It will move if you're modifying the transform. My point is that it's expensive to move static colliders and they won't produce a callback when they collide which is what you're reporting.
     
  5. Leggy7

    Leggy7

    Joined:
    Feb 18, 2014
    Posts:
    21
    Oh, that sounds like what I was looking for.
    I actually move it with the transform. If I understood correctly, moving via transform won't trigger the event? But if I use OnTriggerEnter2D? It should? Just for my understanding, will it work even if it could be more expensive?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    As I said above; when you don't add a Rigidbody2D component then the colliders are static. Static colliders do not collide with each other (no matter what you set-up in the collision layer matrix) and won't produce a callback for collision or trigger. Add a Rigidbody2D to each and set them to be Kinematic. You'll then start to get callbacks.

    In your case, if your wall never moves then you can leave it without a Rigidbody2D (static)but if your arrow moves then you should add a Rigidbody2D component to it.
     
  7. Leggy7

    Leggy7

    Joined:
    Feb 18, 2014
    Posts:
    21
    Thank you very much. This clearified me a lot. I'll try it as soon as I reach home!