Ilan1009
Ilan1009

Reputation: 7

Ground check BoxCast is detecting when player hits bottom of platform. UNITY 2D

I am making an endless platformer.

    private bool IsGrounded()
    {
        RaycastHit2D raycastHit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, 0.02f, platformsLayerMask);
        return raycastHit2d.collider != null;
    }

This is my ground check. But I am having issues. IsGrounded is set to true, when head (top) of player is hitting bottom of platform (Basically Box cast is detecting top even though direction is set to down.

Like this:

Player infinitely jumping under platform

1

Thanks, and please tell me if you need more info! (:

Upvotes: 0

Views: 1421

Answers (2)

Ilan1009
Ilan1009

Reputation: 7

Thank you. Anyways, the platforms were slightly affected by gravity and physics, so they were slightly rotated causing the issue.

Upvotes: 0

Sergio
Sergio

Reputation: 65

RaycastHit2D has field Vector2 normal, you can check if it points up to be sure that the closest geometry is below you. But check if such approach works properly when you stand near the wall (normal can point left or right). In that special case I'd cast one more regular ray down to check for ground.

Upvotes: 0

Related Questions