- Home /
Why Rigidbody 2d doesnt stop sliding?
I have simple script
var h = Input.GetAxisRaw("Horizontal");
if (h == 0)
{
_rigidBody.velocity = new Vector2(0, 0);
}
My expectation that when i dont press "A" or "D" _rigidBody just stay on one point. But it works only on flat ground. When i have hill, rigidbody is sliding down
How can i fix this?
use custom physics for movement this guy make awesome tutorial for 2d game : https://www.youtube.com/watch?v=dwcT-Dch0bA
Answer by Glurth · Aug 11, 2018 at 03:48 PM
I see you have no Physics Material Specified for your Character. Create a new one, and give it a high friction value, then assign it to the character's rigidbody. (Friction is what keeps stuff from sliding down hills in real life too.) Keep in mind, you may want to also assign a material to the hill also! https://docs.unity3d.com/Manual/class-PhysicMaterial.html
Numbers-wise(for some reason not mentioned on that help page): a friction of near ZERO, is slippery, like ice. a friction near ONE is a good solid contact like rubber tires on a road.
@Glurth thanks for the answer. When i have added material like 1 friction to the player colider and 0.7 fritction to the hill colider i still have a little player sliding but now i cant even climb the hill.
Answer by LTonon · Aug 11, 2018 at 07:27 PM
I've seen some solutions in which people do the following: when the player is moving, change the physics material friction to 0. When the player stop moving, change the material friction to some high number (you need to test it out, probably 50 will do, if it doesn't, go higher). This way you can walk normally and when you stop the player doesn't move due physics.
See this answer if you need help in understanding the behavior between the physics materials contact and how to make the player stop on your hills: https://answers.unity.com/questions/790289/how-do-2-physics-material-2d-interact-with-each-ot.html