GameObject *scale*, vs box collider *size* -> Problem with collission

I’m running a 2D project and am attempting to make a 2D square sprite (“Actor”) collide with another 2D square sprite (“Blocker”). However for some reason, the Actor gets blocked when too far away from the Blocker. It’s like there’s an invisible block in between, except there isn’t.

I’m suspecting it has to do with the Actor’s SCALE vs the SIZE of its own box collider. These two values are currently different, because I reduced the scale of the Actor (to 0.16,0.16) to make it better fitting to my camera screen size, while keeping the Box Collider size at (1,1).

I notice that if I reduce my box collider to (0.16, 0,16), the issue disappears. But then it looks really strange because the green outline of the box collider no longer wraps actual Actor (see image below).

What am I doing wrong here? I think I’m messing up the relationships between size, scale and camera size. Btw the camera is set on Size 1 / ortographic, if it makes a difference.

        hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, moveDelta.y), Mathf.Abs(moveDelta.y * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));
        if (hit.collider == null)
        {
            transform.Translate(0, moveDelta.y * Time.deltaTime,0);
        }

        hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.x * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));
        if (hit.collider == null)
        {
            transform.Translate(moveDelta.x * Time.deltaTime, 0, 0);
        }

,A collision notification can be implemented much easier.

Use OnCollisionEnter2D function in your script:

This function checks if any collision has started with the object that has this instruction. If such a collision occurs then the function is called with the collision parameter. From this parameter, you can read the object with which the collision occurred, and consequently, you can read the mask of such an object and compare it with the mask of interest to you.

The box collider size is in local space. So (1,1) will match the object no matter what scale you set your object to.


The boxcast, though I have not used it, appears to be expecting the size in world space, but you are giving it the box collider size which is in local space. You could try passing the collider size vector transformed into world space like:


var size = transform.localToWorldMatrix.MultiplyVector(boxCollider.size);


and pass that as the size vector instead.