- Home /
How would you fake air resistance? (c#)
Ive got a helicopter using two speeds, Helispeed (pitch foward/back speed) and heliRollSpeed (roll left/right speed), But im trying to add a air resistance element to them so you start to slow down if you are leveling out the heli, But cant work out how to add this in well.
This is how i get the speeds (almost identical for ptich/roll):
pitch = heli.transform.eulerAngles.x;
if((pitch > 0) && (pitch < 90)){
float temp = (60 - Mathf.Abs((pitch - 45) / 0.75f)) / 100;
heliSpeed = heliSpeed + temp;
if(heliSpeed > 40.0f) { heliSpeed = 40.0f; }
} else if((pitch > 180) && (pitch < 360)) {
float temp = (60 - Mathf.Abs((pitch - 405.0f) / 0.75f)) / 100;
heliSpeed = heliSpeed + temp;
if(heliSpeed < -40.0f) { heliSpeed = -40.0f; }
}
if(heliSpeed < 0){
heli.transform.Translate(-Vector3.forward * -heliSpeed * Time.deltaTime);
} else {
heli.transform.Translate(Vector3.forward * heliSpeed * Time.deltaTime);
}
But the speed will always increase if you are not at 0 degrees. So i tried adding this to a lateUpdate
if(heliSpeed > 0.01){ heliSpeed -= (heli.rigidbody.drag / heliSpeed); } else if (heliSpeed < -0.01) { heliSpeed += (heli.rigidbody.drag / heliSpeed); }
if(helirollSpeed > 0.01){ helirollSpeed -= (heli.rigidbody.drag / helirollSpeed); } else if (helirollSpeed < -0.01) { helirollSpeed += (heli.rigidbody.drag / helirollSpeed); }
but without thinking, this just means you cant move in any direction, it just shoots you in the opposite direction.
Answer by Tomer-Barkan · Oct 12, 2013 at 05:35 PM
Rigidbodies have a drag value, which is exactly that:
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-drag.html
If you'd want to do this manually for some reason, simply apply force in the direction opposite of the velocity, in proportion to the current speed. If you care about exact formulas, learn about them here:
So ive got this:
drag = 0.5f * 1.2041f * (heliSpeed * heliSpeed) * 0.004f * (4.0f * 4.0f);
Debug.Log("Speed = "+heliSpeed+" -- Drag = "+drag);
if(heliSpeed < 0){
heli.transform.Translate(-Vector3.forward * -heliSpeed * Time.deltaTime);
heli.transform.Translate(Vector3.forward * -drag * Time.deltaTime);
} else {
heli.transform.Translate(Vector3.forward * heliSpeed * Time.deltaTime);
heli.transform.Translate(-Vector3.forward * drag * Time.deltaTime);
}
But it keeps going intil it stops the heli and starts to reverse, how would i change this so it creates a top speed and stays at that?
Again - if you use a Rigidbody, the physics system will do it all for you, that's what its there for and that's what I recommend.
If you still want to do it manually - add to your original code something that slows the speed:
if (heliSpeed > 0) {
heliSpeed -= drag;
} else if (heliSpeed < 0)
heliSpeed += drag;
}
Rigid body drag does nothing, thats why im trying to do a manual version. But that seems to be wrong as-well as, as I lift the pitch you slow down instantly, not slowly like you really would.
If you want rigidbody drag to work use the physics to move the object.
Ins$$anonymous$$d of using transform.Translate()
, use rigidbody.AddForce()
, or even set the velocity manually with rigidbody.velocity = something;
. That way the physics will take care of changing the position, so you don't need to calculate the speed yourself, and also it will take care of drag (make sure drag is not set to 0 though).
Also it will be easier, simply apply the force to transform.up, ins$$anonymous$$d of calculating the pitch manually, as a real rotor would:
rigidbody.AddForce(transform.up * throttle);
Answer by GibTreaty · Oct 12, 2013 at 05:37 PM
I made a post on the forums about applying drag manually with an equation that works exactly like Unity's.
http://forum.unity3d.com/threads/198252-Applying-Drag-Unity-Style?p=1344567#post1344567
Basically this...
velocity -= Vector3.ClampMagnitude(velocity, 1) * drag * Time.deltaTime;
Does unity not take into account the surface area, like in real physics? IE bullet has less drag than a large box...
True but you can still fake it by changing the amount of drag based on your angle (pitch, for example) and velocity. Vehicle drag is done in a similar way in Edy's Vehicle Physics by increasing the amount of drag as your velocity increases.
Your answer
Follow this Question
Related Questions
How to find an intercept on a moving target 2 Answers
How to Reverse an equation.. (c#) 2 Answers
Distribute terrain in zones 3 Answers
Snapping Connection Points Together Rotation 0 Answers
Find Vector3 perpendicular to Vector3 A in direction of Vector3 B 1 Answer