wall check is always returning true even though the distance doesn't fit my parameters :(
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!
Answer by Duke_Hast_Mich · Apr 27, 2017 at 04:44 PM
Well the mathf.infinity on your distance variable on line 4 raises an eyebrow.
Your answer
Follow this Question
Related Questions
c# jumping isn't consistent, please help 0 Answers
Jump Using Raycast. 0 Answers
How can I make a third person camera collision script? 2 Answers
Raycast doesn't hit instantiated object within same frame of instantiation. 0 Answers
Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds 0 Answers