Box Collider2D collide before contact

Hi,

I am using Box collider2D on a GameObjets and its position is moving by code.
I am using OnCollisionEnter2D to detect the collision and stop the translation.

But it looks like the OnCollisionEnter2D event is triggered before the collision occurred and there is a gap between the 2 objects. The size of the object is 0.1 unit. Here a Gif that shows what happens :

Any idea ?
Thanks

First , present some code so that i can help you with your collisions not working properly.
Second, are you using some kind of Raycasting here ?

Hi,
I am not using any Raycasting.
Here the code that stop the object from moving when enter in collision with the wall :

voidUpdate () {
if (jumping == true) {
if (right_side == true) {
transform.localPosition -= velocity;
} else {
transform.localPosition += velocity;
}
}
}

voidOnCollisionEnter2D(Collision2Dc){
if( c.gameObject.tag == “wall” && jumping == true ){
right_side =! right_side;
jumping = false;
}
}

Have you tried maybe adding a trigger object childed to the moving object, then check for the trigger on that object, that may allow you more control over the actual location of the trigger

Not 100% sure - but I’ve seen this mentioned multiple times in the forums. I believe this is what you get with the 2D physics engine.
Maybe there is a real fix via code bits, but I’ve heard some say scaling up the sprite to be a little bigger than the collider has also been a quick fix.

Update with the solution you end up using.

check #8

Indeed, make your objects (and colliders) bigger.