- Home /
Question by
typingbacon · Aug 08, 2020 at 04:47 AM ·
physicscheckgroundgroundedground detection
Why does this type of ground check not working?
So the game that I'm currently working on takes place on a flat surface, the range of my ground is within that if statement. The rigid body gravity is not satisfying enough since the scaling of the asset I'm using is wack so I went on ahead and made my own gravity but the problem is when I'm on flat ground vertical velocity still keeps accumulating and for some reason the if statement is not working.
if (position.y == 0.5548794 || position.y == 0.5548793 || position.y == 0.5548792 || position.y == 0.5548791 || position.y == 0.5548790)
{
vertVel = 0;
GetComponent<Rigidbody>().velocity = new Vector3(horizVel, vertVel, 3);
}
else
{
vertVel = vertVel - ((float)grav * Time.deltaTime);
GetComponent<Rigidbody>().velocity = new Vector3(horizVel, vertVel, 3);
}
Comment
Your if may not be working because there is a small degree of imprecision in floats and you are checking too small of detail. Try: if ($$anonymous$$athf.Approximately(position.y, 0.5548794f)) or a range: if (position.y < 0.5548794f && position.y > 0.5548790f)
Your answer
![](https://koobas.hobune.stream/wayback/20220613005146im_/https://answers.unity.com/themes/thub/images/avi.jpg)