Why my rigidbody2D.velocity.y has a value of -0.1962 even though I'm applying a velocity of 0 to it?

I posted this question in another community, but no one has answered me, so I will give a try here :slight_smile: This is the thing:

My main attempt is to avoid my character to slide down in a slope, all this using the Physics 2D motion (not using the raycast system). Neither making my rigidbody2D iskinematic nor putting a big amount in the friction material are alternatives, although they might work but they also bring me more troubles.

So I decided to research why my character slides for the slope even thought I set the velocity.y attribute of the rigidbody2D to 0, as far as I know it should not make my character to fall, but it does and for that rason my character still falling.

I apply the next code to get the velocity.y when I apply 0 to velocity.y:

Rigidbody2D rb;

void Start () {
  rb = GetComponent<Rigidbody2D>();
}

void FixedUpdate () {
  Debug.Log(rb.velocity.y);
  rb.velocity = new Vector2(rb.velocity.x, 0);
}

This are the results:

  • In the first FixedUpdate, Debug.Log(rb.velocity.y) prints 0, something logic because at these point no force is being applied yet.

  • But since the second ejecution of FixedUpdate, Debug.Log(rb.velocity.y) prints the magic number of → -0.1962 (This value might change depending of the configuration of the gravity, linear drag, and other stuffs involving in the performance of the gravity, I used the defaults values for get -0.1962)

Knowing the magic number, I used his inverse (absolute value of -0.1962 that is 0.1962) to nullify that little force that is being applied somewhere in the pipeline of the Physics2D… And it actually works!! :

void FixedUpdate () {
  Debug.Log(rb.velocity.y);
  rb.velocity = new Vector2(rb.velocity.x, 0.1962f);
}

Now I can make everything (using the gravity of -9.81, for other gravity value I would need to find the new magic value of the new gravity force) levitate!

But the questio (for ALL these) is: What means that magic value (in my example -0.1962)? Where it occurs? And why?

Even if I’m telling Unity not to apply any force to the rigidbody2D, Why they actually apply that force behind the scene? (Cruel Unity! I dont understand you :c jaja) I’m pretty sure it happends in the Internal Physics Update (Read here further information). Just someone explainme WHYYY??? (So I could know how to code better in these situation)

I will appreciate any help.
Thanks c:

Check you aren’t applying Gravity. Literally had this same problem a moment ago, I was getting unwanted downward motion because I didn’t have gravity set to 0.

It doesn’t do everything at the same time, it will do things one at a time so it sets it to 0 then applies gravity then sets it to 0 then applies gravity etc etc

The physics system runs after you get the FixedUpdate callback. It doesn’t matter that you reset the linear velocity zero; if you have gravity affecting the object then it’ll be applied. -0.1962 will be the Y-axis component of the downward force with the rest of the force being in the X-axis because, presumably, this is on a slope.

If you don’t want gravity then why not set the gravity scale of the Rigidbody2D to zero when in contact with a slope? You get the collision normal(s) so you should be able to determine that.

Alternately, you can leave gravity scale as it is and apply a force opposite gravity in your FixedUpdate callback. This force is -Physics2D.gravity * rb.gravityScale * rb.mass (note the minus sign).

2 Likes