- Home /
Check if rigidbody is falling
Hey, Im trying to get my rigidbody to stop its code when it is falling, so i tried an if rigidbody.velocity.y is != to 0, this did work in checking if the player is falling or not, but then my other code could kick in. In the code it used rigidbody velocity to move left and right, NOT up and down, but this still trips the if velocity.y statement. how do I get around this?
I'm not sure if I follow your question, but it sounds like you might need to use a threshold value and a < comparison, e.g. 'if (velocity.y < fall_threshold)', where 'fall_threshold' is a negative value. This way, slight variations in the y velocity (e.g. due to numerical error) won't be registered as 'falling'.
Thats what I know how to do, the real problem is the velocity.y is influenced by moving the rigid-body on the x axis?
I would say that the friction on the rigidbody causes some slight changes on the y axis of it's velocity. If this means you are getting small fragments in there, you will indeed need to check for something like Jesse said, as the falling velocity will be a negative value.
Answer by Jason Hamilton · Jun 25, 2011 at 04:37 AM
I dont know how I this works but I moved my 'check the rigidbody y velocity' if statement to a late update and it works fine! maybe because it is called after the move velocity? :)
LateUpdate() is called neither before nor after physics simulation. LateUpdate() happens after Update() within the course of a single frame. Physics simulation, on the other hand, occurs after every interval of Time.fixedDeltaTime.
Answer by RickshawDerpyDerp · Jul 16, 2019 at 11:48 AM
Isn't working for me. All I am testing at this point is
print(rb.velocity.y < 0);
And my debugger spits out nothing but "false", "false", "false", "false",... whether I put it in Update(), LateUpdate() or FixedUpdate() and jump around. Absolutely ridiculous.
Answer by the1whom0x · Mar 29, 2020 at 05:17 PM
I'm late to the party but, comparing floats with != and == is not accurate.
Using Mathf.Approximately(float1, float2) is much better, and would fix your issues.