boxcast not working?

i’m trying to add collision to my character in 3D, and for some reason my boxcast is not returning true even though I have the ground layer set to “ground”, and whatisGround is set to the proper ground layer mask.

Here’s my script

public class PlayerMovement : MonoBehaviour {

    public Collider m_Collider;
    public RaycastHit m_Hit;

    public void Awake ( ) {
        m_Collider = GetComponent <Collider> ( );
    }

    public bool IsGrounded ( ) {

        // Fetch the center of the Collider volume
        m_Center = m_Collider.bounds.center;

        // Fetch the size of the Collider volume
        m_Size = m_Collider.bounds.size;

        m_SizeY = ( ( m_Size.y / 2.0f ) + 0.01f );

        m_Min = m_Collider.bounds.min;
        m_Max = m_Collider.bounds.max;

        var colliderExtentsY = m_Collider.bounds.extents.y;
        var rayPosition = ( m_Center );
        var rayDirection = ( -transform.up * m_SizeY );

        isGrounded = Physics.BoxCast ( 
            rayPosition, m_Size, 
            rayDirection, out m_Hit, 
            transform.rotation, m_MaxDistance, 
            whatIsGround
        );

        return isGrounded;

    }

    public void Update ( ) {

        Debug.Log ( 
            $"IsGrounded ( ) : { IsGrounded ( ) }"
        );

    }

}

Any help is absolutely appreciated!

You probably want to use OverlapBox, not BoxCast. If BoxCast (or any of the casts) are already overlapping a collider it won’t count as ‘hitting’ it.

1 Like

It’s not overlapping any collider. it’s overlapping a layer mask ( ground ).

That doesn’t make sense as a reply TBH. You don’t overlap a layer-mask. A layer-mask is an argument specifying what you want to detect you overlap with.

The above suggestion was to use the OverlapBox test which gives you a yes/no answer rather than the cast which as also mentioned, won’t detect things it starts overlapped with.

1 Like

I don’t see any problem with using Boxcast. I will try Overlap, but right now I just want to know how to properly use Boxcast in my example.

I’m teaching myself casting collision methods though. So I would like to know how to fix what I already have.

You’ve been told plenty. If your box cast starts already overlapping with the ground, it won’t detect a hit with the ground.

It goes without saying that the ground needs to have a collider on it too.

2 Likes

Well, this was said…

Help is of no use if the replies are not read. :frowning: If a particular point isn’t understood then always feel free to ask about the reply itself or state you don’t follow. This stuff can be confusing for sure but there’s gold in the replies with little digging. :slight_smile:

2 Likes