- Home /
How drag is calculated by Unity engine.?
Hi all,
Do anyone know, how Unity Engine calculates drag for rigidbody which is applied by a force.
I have car and set drag as 0.35 in rigidbody setting. Is that calculated proportional to the velocity, if so could you share the formulae used.?
Is it DragForce = DragCoefficient * Velocity .?
Please help me.
Regards, anu
Answer by yavilevich · Dec 06, 2015 at 02:52 PM
Based on what I have tested, though in real physics, drag is calculated as
dragForceMagnitude = velocity.magnitude ^ 2 * drag;
in Unity a linear approximation is used. Where on each FixedUpdate
velocity = velocity * ( 1 - deltaTime * drag);
This is based on the following forum posts: http://forum.unity3d.com/threads/drag-factor-what-is-it.85504/ and http://forum.unity3d.com/threads/physics-drag-formula.252406/
So the change on velocity at each FixedUpdate is
vNEW = (v+a*dt)*(1-drag*dt)
f = m*a
The result of this is that, given some constant force, an object with drag will reach a certain max velocity (vNEW=v). The calculation of the formula of such velocity (vSaturation) is included below.
we will assign v=vSAT and vNEW=vSAT and get:
vSAT = (vSAT+a*dt)*(1-drag*dt)
vSAT*drag*dt = a*dt - a*drag*dt^2
vSAT*drag = a - a*drag*dt
vSAT = a/drag - a*dt = f/m/drag - a*dt
so if force is 100, mass is 1, drag is 1 and dt=1/50 we get
vSAT = 100/1 - 100/50 = 98
$$anonymous$$ight I ask why you have the equation
velocity = velocity * ( 1 - deltaTime * drag);
When in this link (you linked to) its called acceleration?
https://forum.unity3d.com/threads/physics-drag-formula.252406/
Don't get me wrong, I think you're correct because in Nickdas' physics script he applies acceleration to the position of the object which sounds incorrect to me. I think so because in any other equation I've found to find velocity it is
v(t) = v + a*dt
The last thing I wanted to ask is probably pretty simple but I am unsure. Is 'a' dependent on 't' or is 'v'?
Answer by CodeElemental · Feb 28, 2014 at 12:49 PM
Drag affects the object's velocity , approximately with the formula :
dragForceMagnitude = velocity.magnitude ^ 2 * drag;
dragForceVector = dragForceMagnitude * -velocity.normalized;
If you want to know more , visit the forums , from where i actually got the info.
Also , you can maybe find this table for drag coeff for primitives usefull.
Answer by RicBrasil · Feb 19, 2021 at 07:58 PM
Hi guys, I ran into this problem and looked it up for a solution. And didn't find one on unity space equation. Basically I needed to launch an projectile to certain point in space given the time "t". It also works for 3D if you use Vector3. In my case I'm simulating wind by changing the component x of the gravity. So I end up finding in a physics site about linear drag. And I came up with this:
Vector2 a = Physics2D.gravity * shotRigibody.gravityScale;
Vector2 deltaS = (Vector2)(_target.position - shotInstance.transform.position);
float t = 2f;
if (rigibody.drag > 0)
{
float drag = rigibody.drag;
float tMax = 1 / drag;
float et = Mathf.Exp(-t / tMax);
Vector2 vt = a / drag;
rigibody.velocity = (deltaS - vt * t - vt * tMax * (et - 1)) / (tMax * (1 - et));
}
else
{
rigibody.velocity = (deltaS - a*t*t /2 )/ t;
}
This physics site uses F = m*g - b*v while unity uses F = m*g - m*drag*v. So you can replace b for drag*m.
Your answer
Follow this Question
Related Questions
Horizontal Drag, no Vertical Drag 1 Answer
Limiting the top speed of a rigidbody in 3d space? 1 Answer
Why is there still drag when drag is 0? 0 Answers
Velocity Formula 1 Answer
Velocity powered rigidbody on a moving platform without parenting. 3 Answers