So I’m trying to implement a wall check in my game and I have the raycasters working just fine. They return the correct distances and they are recognizing the correct layers, but the .distance and .fraction < whatever number I choose is always returning true no matter what the actual distances are.
Here’s my code:
void FixedUpdate()
{
hit = Physics2D.Raycast(rigi.position, Vector2.down, 5f, LayerMask.GetMask("Ground"));
hitLeft = Physics2D.Raycast(rigi.position, Vector2.left, Mathf.Infinity, LayerMask.GetMask("Wall"));
hitRight = Physics2D.Raycast(rigi.position, Vector2.right, 5f, LayerMask.GetMask("Wall"));
IsGrounded();
IsWalled();
if (hitLeft.collider != null || hitRight.collider != null)
{
if (hitLeft.distance < 0.3f && grounded == false)
walled = true;
else if (hitRight.fraction < 0.3 && grounded == false)
walled = true;
else walled = false;
}
Debug.Log(hitLeft.distance);
Debug.Log(hitLeft.collider.name);
Debug.Log(walled);
}
The grounded check is fine. Walled returns false when I’m on the ground. But, whenever I’m in the air regardless of the distance from any walls, walled always returns true. Please help! Thank you!