Physics2D BoxCast: What is the returned distance?

I couldn’t figure this out from the docs. When you do a BoxCast2D, the returned RaycastHit2D has a distance.

BoxCast2D doesn’t specify what the value of that BoxCast is. Is it;
1: the distance the virtual box travelled before the intersection
or
2: the distance from the origin point of the ray to the intersection point (ie. the same as Vector2.Distance(rayOrigin, hit.point)?

The documentation for RaycastHit2D implies 2. I could test this (in fact I will!), but this should be possible to know by reading the documentation!

I concur… you should ping the suggestion button at the bottom of the docs.

However, based on this line:

“It also returns the centroid where the box would be positioned for it to contact at that point.”

and this line about the input “Distance” value:

Distance controls how far from the origin the Box travels.”

I am going to guess that perhaps the distance in the hit2d will be #1 above.

It was 1!

1 Like

We desperately need diagrams for physics query arguments and the return values.

1 Like

I would knee-jerk agree with this but upon thinking on it another five seconds, if you had those diagrams, then they could become untrue when things change. Documentation rots quickly in a fast-changing environment!!

Instead, prove it! Unity just makes stuff SO easy to rip out a quick test, you can instantly prove it for yourself what is happening. new up a GameObject, throw a BoxCollider2D on it, throw it to the right, make a Raycast, make a BoxCast, print the results, and know for sure.

using UnityEngine;

// @kurtdekker
// empirically testing what "distance" means with 2D BoxCast
// to use, drop this on a GameObject and run

public class ColliderContactCastDistances : MonoBehaviour
{
    void Start ()
    {
        Vector2 position = transform.position;
        Vector2 direction = transform.right;

        // target will be made to the right of wherever we are
        Vector2 targetPosition = position + direction * 3.0f;

        // make something to hit
        GameObject Target = new GameObject("Target");
        Target.AddComponent<BoxCollider2D>();
        Target.transform.position = targetPosition;

        bool didHit = false;
        RaycastHit2D hit2d = new RaycastHit2D();

        // try a few casts of varying distances and types
        for (float distance = 1; distance < 4; distance += 1.0f)
        {
            hit2d = Physics2D.Raycast(
                origin: position,
                direction: direction,
                distance: distance);

            didHit = hit2d.collider;

            Debug.Log( System.String.Format( "Ray: didHit:{0} distance = {1}", didHit, hit2d.distance));

            Vector2 size = new Vector2( 1, 1);

            hit2d = Physics2D.BoxCast(origin: position,
                size: size,
                angle: 0,
                direction: direction,
                distance: distance);

            didHit = hit2d.collider;

            Debug.Log( System.String.Format( "Box: didHit:{0} distance = {1}", didHit, hit2d.distance));
        }
    }
}

For instance, TIL there is a certain amount of floating point slop in BoxCast:

7917274--1010101--Screen Shot 2022-02-22 at 7.11.23 AM.png

I need to fiddle more 2D physics games… I always have fun when I do.