- Home /
Horizontal Drag, no Vertical Drag
Hi, I was wondering if it's possible to have different values for the drag of a rigid body, I'm currently moving a character using
rigidbody.SetVelocity
Using high drag my character doesn't slide around but this means if my character jumps it doesn't fall at free fall speed which I would like.
Doesn't anyone have a solution to this?
Thanks
Answer by robertbu · Nov 11, 2013 at 03:59 PM
The best solution is to backoff the drag setting and instead keep your objects from moving around by assigning the right physic materials to the colliders.
http://docs.unity3d.com/Documentation/Components/class-PhysicMaterial.html
You might get what you want just by upping the setting for gravity to compensate for the friction. Go to Edit > Project Settings > Physics and set the 'Y' value for gravity to a larger negative number.
Alternately you could play with doing your own drag. Here is a simple version. Set the drag in the Rigidbody back to '0' and then place the following two lines in FixedUpdate():
rigidbody.velocity.x *= 0.985;
rigidbody.velocity.z *= 0.985;
If you are writing in C#, you will need to use a temporary variable for this calculation and then assign the result back.
I'm not sure I fully understand what's going on here with setting the rigidbody velocity to *= 0.985
Could you possibly explain this a little bit?
The line of code expanded out is:
rigidbody.velocity.x = rigidbody.velocity.x * 0.985;
The '0.985' is just a guess at an appropriate drag setting. You will have to play with this number. The idea is that at each frame, the velocity will decay. So if the x component of the velocity is 2.0, it then would decay to 1.97 and then the following frame it would decay to 1.94 and so on.
Thanks I understand it now, works just like any deceleration, dunno why I couldn't see that before (facepalmed)
I appreciate your help, although eventually I just set x and z velocity to 0 when my analogue sticks magnitude was below a certain threshold XD
I'm not sure if I should mark your comment as answered because I didn't manage to get it to work :| regardless I'll thumb you up for your time :)
Thanks again!