Home > Enterprise >  Why is the Y velocity in rigidbody2D changing with no reason?
Why is the Y velocity in rigidbody2D changing with no reason?

Time:07-15

I'm making a 2D game where the player is able to jump only when the Y velocity==0, the player moves only on the X axis so I don't see a reason for the Y velocity to either decrease or increase. The problem is that it's weirdly changing. Please help me fix that RigidBody2D velocity's problem, it's making me crazy. Thanks. I'm showing you how the velocity is changingThe Colliders of the objects that are causing this problem

edit: I restarted unity but it didn't work, even when I'm not moving the Y velocity is increasing.

CodePudding user response:

You should'nt compare float numbers because they are not fully precise.

Check IEEE 754 and instead of comparise them, check if it's greater than some value. Also look at you're y value in the rigidbody component to be sure.

CodePudding user response:

There are two solutions:

  1. Check if the speed y is very small, as they suggested.
  2. Use Freeze Position Y. By activating that box and deactivating it when you have to move vertically, for example to jump, you will be constrained to the Y axis, and therefore the speed will not change.

Good work!

  • Related