BoxCast2D Distance not working

Hello,

I’m expierincing a problem using the BoxCast2D function in Unity.
I’m using this Method:
RaycastHit2D BoxCast(Vector2 origin, Vector2 size, float angle, Vector2 direction, float distance, int layerMask)

Physics2D.BoxCast(transform.position, new Vector2(2, 1), 0, Vector2.down,1, obstacleAndEnemyLayer);

setting up my Box works fine and the collisions are always detected, but when i adjust the distance from 1f to 100f nothing seems to change. the checked area stays always the same…

can anyone tell me what I’m doing wrong?

thanks in advance!

[quote=“MystikMasi, post:1, topic: 892786, username:MystikMasi”]
but when i adjust the distance from 1f to 100f nothing seems to change
[/quote] If you are referring to the shape, then nothing should change. By changing “distance” you are only increasing (in this case) the length of the cast.

Documentation:
distance = The maximum distance over which to cast the box.

Hello lightbug14,

thanks for the fast reply. I know that the shape of the box stays the same but I expected when I change the value distance from 1 to 100 the box will be casted 100 Units down.

Look at the image I want to detected the GameObject marked with an X. But it just returns a collision when the GameObject is inside the Box. I tried different values for the distance but every time it just detects the collision when inside the defined Box.

Do the cast and then print/debug its information (it returns a RaycastHit2D). Something like this:

RaycastHit2D info = Physics2D.BoxCast(....);
bool hit = info.collider != null;
print($"hit = {hit}");
if (hit)
{
     print($"hit distance = {info.distance}");
     Debug.DrawRay(info.point, info.normal, Color.green);
}

That’s the only way to know if it is working or not (spoiler alert, 99.999% of the time it works). In fact, How do you know the cast is not working now? Maybe you’re expecting to receive a hit with X, but the cast is hitting something else. So when X is close enough then your code recognizes a hit.

Is the BoxCast overlapping with an existing collider (e.g. a box character doing casts)? If so, the boxcast will detect that box collider (let’s call it “Character”) as THE hit, and obviously is gonna ignore anything else. It could be that by putting X closer (inside the box), the hit recognized by the query changes from “Character” to “X”. You could move from BoxCast to BoxCastNonAlloc and handle each hit however you want.

this is how my code looks like:

RaycastHit2D hitInfo=Physics2D.BoxCast(transform.position, new Vector2(2, 2), 0, Vector2.down,10, obstacleAndEnemyLayer);
            if(hitInfo.transform!=null)
            {
                Debug.Log(hitInfo.transform.tag);
                if(hitInfo.transform.CompareTag(enemyTag))
                {
                    Debug.Log("Attack");
                }
            }

and when I’m dragging my GameObject in Scene View around nothing is happening only when I put my GameObject inside the Box I receive the Debug Message. like i said before changing the distance doesn’t change anything.
But I want to Cast the Box down and this is not working… I tried this in the past in my other projects and it was always the same.
even Math.Infity doesn’t change the Area where the collision happens…
By the way, my Layermask is set to Layer „Player“ and only my player GameObject is on this Layer.

The code does not indicate anything about the hit. The only way you will ever get something (assuming this is all you’ve tried) is by getting an “Attack” log in the console.
If you try the code i made, you will have precise information about the hit. Also you could try to debug the game (better than printing stuff).

Ok, that means the player is not interfering. However, some other colliders or even a trigger could be causing trouble.

Yes that’s right! Will try this next time.

I tried to call an animation when the collision was detected to visualize it better.
The only solution I could find was setting up the Box Height instead of using distance(I created a box that is big enough to reach the area I want to check) But this is not how it should work…

in my last project I visualized everything with debug rays and ended up using Raycast instead.

I did hours of debugging and testing before I posted this thread.

Hello Again,

So i took the time today to update to the latest Unity version and bought the EasyRaycastVisualizer2D.
Debugging just shows my what I already knew…
Dragging my GameObject inside the Boxcast(red box/line) nothing happens. Only when I drag my player inside the origin Box, the BoxCast detects the collision and the Box turns green.

any Ideas what I’m doing wrong?