- Home /
Jumping boolean check
I seen to hit a bug and I'm not sure how I go about fixing in. I've create a jump within the game which allows the sphere to go up couple of units as shown below, however if I jump and hit a wall my boolean check gets stuck and I'm unable to jump again for a couple of seconds. I've noticed that rigidbody.velocity.y doesn't always return 0 and returns something -7.4050503E or something i've tried Mathf.Approximately(rigidbody.velocity.y, 0) but problem still occurs, any tips?
if(grounded == false && rigidbody.velocity.y == 0) {
grounded = true;
}
if(Input.GetKeyDown(KeyCode.Space) && grounded == true) {
rigidbody.AddForce(Vector3.up * jumpSpeed);
grounded = false;
}
A "grounded" variable should almost certainly use a Raycast "down" to deter$$anonymous$$e its status.
Also as you mentioned, checking for equality on floats is a recipe for bug soup. Checking for approximate is safer.
Sorry i'm new to unity as a whole, I haven't heard of raycast before. Do you $$anonymous$$d giving me an example?
Answer by Doodlemeat · Aug 28, 2014 at 03:30 PM
This question has already been answered. http://answers.unity3d.com/questions/196381/how-do-i-check-if-my-rigidbody-player-is-grounded.html
Answer by robertbu · Aug 28, 2014 at 03:30 PM
The second example on this page is testing for the distance to the ground:
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html