- Home /
Overcome drag
I want an object to be able to accelerate to a certain speed at a given time despite the drag. The problem is in a way that drag acting on a rigidbody, decreasing it's velocity each frame by some value.
void FixedUpdate()
{
Vector3 v = throttle * MaxSpeed * Direction;
Vector3 dv = v - _rb.velocity;
float t = TimeToSpeedUp * (dv.magnitude / v.magnitude);
if (t > 0f)
{
_rb.velocity += dv / t * Time.deltaTime;
}
}
So, let's say I want to reach MaxSpeed in TimeToSpeedUp seconds. Everything seems to be normal while the drag is zero, but if it's not, how I could compensate velocity?
Answer by lgarczyn · Nov 02, 2019 at 07:08 PM
The simple solution would be to simply switch between a physics material with and without drag.
The other solution is to calculate the drag force applied on the next physics frame, and apply the reverse beforehand. To calculate the drag approximation used by unity, this answer provides more info: https://answers.unity.com/questions/652010/how-drag-is-calculated-by-unity-engine.html
Calculating the drag force solution appears to work well.
Your answer
Follow this Question
Related Questions
Help with DragRigidbody.js ? 1 Answer
drag object including free rotation 0 Answers
Where is the general Rigidbodies Drag? 3 Answers
How to Tilt a Spaceship Based on Inertia/Acceleration Force Separate from Facing Direction? 2 Answers
Is there any way to apply drag to a 3d rigidbody in only 1 direction? 2 Answers