- Home /
How can you make gravity to not have any acceleration but with const velocity?
So i want my player to fall with const velocity and no acceleration without to disable the rigidbody gravity and apply a const force every frames. I heard that you could achive this by changing the linear drag to some specific number and i also want to know why?
Answer by Bunny83 · Nov 23, 2019 at 01:04 PM
Gravity is a force and therefore applies an acceleration on the object. However in the real world things also do not get infinitely fast when they are "falling" for a long time. Every object has a terminal velocity which is based on the drag form factor of the object, the projected area and the density / viscosity of the fluid the object falls through.
Since simulating real world physics is most the time way too complicated we generally use a simplified physics model. So PhysX / Unity does not have a concept for density, viscosity, form factor or projected area. The drag value of your rigidbody essentially combines all this into a single constant value. Also the drag force in Unity does not depend on the squared velocity but just on the linear velocity.
I've written some helper methods which allows you to calculate the required drag for a given acceleration and wanted terminal velocity. However keep in mind that you do not instantly reach that terminal velocity. In theory you will never reach the terminal velocity since the effective acceleration just gets smaller and smaller over time and you get closer and closer to the terminal velocity.
What you essentially need is the GetDrag and GetDragFromAcceleration methods.
public static float GetDrag(float aVelocityChange, float aFinalVelocity)
{
return aVelocityChange / ((aFinalVelocity + aVelocityChange) * Time.fixedDeltaTime);
}
public static float GetDragFromAcceleration(float aAcceleration, float aFinalVelocity)
{
return GetDrag(aAcceleration * Time.fixedDeltaTime, aFinalVelocity);
}
And use it like that once in Start:
rb.drag = GetDragFromAcceleration(Physics.gravity.magnitude, yourTerminalVelocity);
Or if this creates issues with your jump logic you may want to set / reset it dynamically based on where the player is within the jump cycle. So for example if the velocity.y is positive (jumping up) you set drag to 0 and when falling (velocity.y is negative) you set it to the calculated drag value.
Thanks for the help. I also want to flip the player's gravity like in vvvvvv so do you think this drag would work well with it?