- Home /
Calculate Terminal Velocity
The scenario is: 2D, movement on XY. No Gravity, object has a drag value of .6 and has a force of vector.up * 5 being applied to it (FixedUpdate(),Forcemode.Force).
How would I calculate the terminal velocity or max velocity of this object ahead of time?
Looking at the normal formula, I don't really have a projected area of the object or fluid density in this case. I'm also not really sure if the rigidbody.drag value is the same thing as the Drag Coefficient in this formula.
Thanks!
I think you'd have to apply your own drag forces for you to be able to reliably calculate this.
Answer by Bravini · Feb 27, 2011 at 02:56 PM
i'm not very good at math, but you can limit the max speed of you rigidbody, so you know beforehand what it will be. Code is as simple as:
public float maxVelocity;
void FixedUpdate() {
if (rigidbody.velocity.x > maxVelocity)
rigidbody.velocity.x = maxVelocity;
//now for the reverse direction
else if rigidbody.velocity.x < -maxVelocity )
rigidbody.velocity.x = -maxVelocity;
}
Tested code, I use this to limit my AI chars from moving too fast.
Actually Bravini, you code won't work. You are comparing a Vector3, velocity, to a float, maxVelocity. $$anonymous$$aybe you meant velocity.magnitude, but that still leaves you to fix the statement inside the conditional blocks.
thanks for the heads up, just fixed. That's what happens when you're too lazy to actually open the project and look :]
You're just limiting your speed in x direction, Unity have a nice function that can clamp a Vector3: Vector3.Clamp$$anonymous$$agnitude().
yeah, it works the same too, but using variables you can set different limits for forward and backward in the inspector or in runtime with boosters, in case you want to move a car or a spaceship with thrusters.
Answer by Alex Hogan · Feb 27, 2011 at 09:46 PM
Rather than calculate it, why not set it?
Make a BehaviourScript with two parameters - terminal velocity and a threshhold.
In update, or maybe LateUpdate, check the rigidbody's velocity.
If it's within the threshhold, start increasing the drag.
Use InverseLerp with the ThreshHold value and the TerminalVelocity value to get the amount of drag you want to add - just make it go to one as it approaches the terminal velocity.
The problem is that drag isn't clamped to 1, its clamped to infinity and that will completely stop your object. So the challenge is deter$$anonymous$$ing the proper drag to stop the object from accelerating, but not slow it down any.
Answer by _MGB_ · Feb 28, 2011 at 08:11 PM
Expanding on my comment above... Set the Unity body drag to zero and then calculate and apply your own drag forces - it's not too hard. Then you know the exact values used and can plug them into this equation
Answer by AVividLight · Mar 02, 2011 at 06:34 PM
I'm not sure if it will help, but you could modify my code, to get the speed by knowing the distance and speed...
//Written by/for Gibson Bethke
var Counter : float = 0.0; var maxCounter : int = 0; var terrain : Terrain; var Player : Transform; var Guitext : Transform;
function Update () {
var up = Player.TransformDirection (Vector3.up);
var hit : RaycastHit;
Debug.DrawRay(transform.position, -up * 5000, Color.green);
if(Physics.Raycast(Player.position, -up, hit, Mathf.Infinity)){
Counter = Vector3.Distance(Player.position, hit.point);
if (Counter > maxCounter) maxCounter = Counter;
if(Counter <= 10){
Guitext.guiText.text = "You're at ground level Top Height: " +maxCounter;
}else{
Guitext.guiText.text = Counter+ " Meters";
if(Counter >= 10){
Guitext.guiText.text = Counter+ " Meters Top Height: " +maxCounter+ " m";
}
}
}
}
Or just make a 2d image with even number intervals and time it...