I posted this question in another community, but no one has answered me, so I will give a try here
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: