RaycastHit2D being detected even colliders are overlapping (Start Queries In Colliders disabled)

Hello,

Is there any reason behind this RayCastHit2D being detected even the ball is overlapped by the black square?

As you can see in the debug console, the first value is the distance between colliders and the second value says if the colliders are overlapping or not. I thought that disabling ‘Start Queries In Colliders’ option would avoid this behavior, but it seems to not be working as I was expecting. Why is this happening?

I have also observed that something similar happens on the opposite side where the RaycastHit2D detection distance ends (distance = 1f):

When the distance is higher than the observed 0.986f value, the RaycastHit2D is not being detected even visually the ball is inside the detection area:

Are these 0.013-0.014 missing units expected? Where are they being missing?

Thank you very much in advance!

Kind regards

There’s almost always a reason.

Since you showed no code, little more can be said except,

  • check the docs
  • debug what you have and find out what is happening.

Ops, sorry! Here you have the code:

public class Test : MonoBehaviour
{
    private BoxCollider2D boxCollider2D;

    private void Awake()
    {
        boxCollider2D = GetComponent<BoxCollider2D>();
    }

    private void FixedUpdate()
    {
        RaycastHit2D[] hits = new RaycastHit2D[2];
        int hitCount = boxCollider2D.Cast(Vector2.right, hits, 1f);

        if (hitCount > 0)
        {
            var collider = hits[0].collider;
            Debug.Log($"{collider.Distance(boxCollider2D).distance} and {collider.Distance(boxCollider2D).isOverlapped} and hit point {hits[0].point}");
        }
    }
}

Thanks!

I have also checked the docs, but I do not find anything explaining why casts are detecting hits when ‘start queries in colliders’ is disabled and the overlap is happening :frowning:

Any help here, please? :frowning:

If any of the objects are dynamic then the physics engine will move them so they’re no longer overlapping and that’s when your boxCollider.Cast will be able to detect the no longer overlapping object.