- Home /
Jumping is slightly faster than running due to friction
I have a 2D character controller that uses addForce to run and sets a clamp to limit run speed. It all works great on the ground.
But - due to friction, my character moves slightly faster on the x axis while airborne. So for example, my x velocity will be 13.8 on land and 14.0 in the air. I need tips on how I can prevent this. I'd like them both to be exactly the same (like in mario, or any other platformer worth its weight in salt).
Answer by andzq · Oct 19, 2016 at 01:49 AM
just apply the friction while u´re not holding a movement key.
This worked out for me, but a warning to others who are looking to attempt changing friction via script in 2D unity - it is buggy and needs a workaround. I finally found the workaround here:
http://answers.unity3d.com/questions/949087/changing-friction-from-code-not-working-2d.html
I always just do it while the player is in the air (i.e. not grounded). This way a release of the key doesn't slow down the player's sliding on the ground and you keep that nice mario effect.
Can you explain how did you apply the friction on the air? Do you tweak the drag/angular drag on air?
@ Naphier
The way I'm doing it, the player is at 0 friction while running. So I HAVE to change friction while he's on the ground, otherwise he will slide for eternity. Even with friction turned back on, he doesnt stop immediately though. It is working quite well.
I wouldn't even have to fiddle with the friction at all if he wasn't faster in the air than on the ground.
I see, I wouldn't do 0 friction when running, just apply enough force so that my running looks correct. But everyone does this a little different. I'm not sure why he would move faster in the air though, maybe he has a physic material with friction too (or the ground does)? When my players are in the air they don't move faster, but they can accelerate because I'm still using AddForce while they're in the air, but I just have the script double their drag while in the air and it's not problem. Looks great too.
I see. Yeah there is no drag applied to my player, so the player is always faster in the air, because friction comes into play on the ground. You slowed him in the air to fix it, I sped him up on the ground by removing friction while in motion. I will fool around with your solution and see if that works better for me, but right now my movement works great.
Not sure if it really is though. If you think about it, we should probably strive to model after real life physics as much as possible. So really the ground should have a physic material and the player too, these would create the normal ground drag, but while in the air we shouldn't be applying any new force, or if we do the force should be much smaller than the movement force. But it's a tough one because game physics (espt 2d platformers) are not very realistic. So the simplest solution wins, or the one you think of at the time!
Right its not so much realism I'm going for as it is robust implementation that will hold up in all kinds of conditions down the line. Zeroing friction while in motion just doesn't sit well with me in that regard.
Btw all colliders have a default friction of 0.4 if there is no material attached, so don't worry about adding a material to everything that touches your character.
Your answer
Follow this Question
Related Questions
2D Rotate towards GameObject acting odd. 2 Answers
Trying to move an instantiated object with a Rigidbody2D and AddForce 1 Answer
Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer
issue with the rigid body x axis ,addforce doesn't work as it should for x axis 1 Answer
Adding force to rigidbody2d to slide 1 Answer