I’m creating a simplified space flight sim where the player is given velocity by applying a force from the back of the craft and the rigidbody is given a drag value so that if the engine is shut down the craft’s forward velocity drops. I just realized I want the player to be able to match the velocity of an enemy spacecraft so they can dogfight (think Tie Fighter a la '94ish).
Does anyone know a formula I can use to determine how much force I need to apply to my spacecraft (given the constant drag factor I’ve applied), to match a target’s velocity?
Thank you!
ps. I apologize if this has been asked before, but I couldn’t find any posts addressing this.
I’m no expert but how about simply copying the velocity from one rigidbody to another?
Wouldn’t that make it a whole lot easier?
I don’t see why you shouldn’t be able to do that, but I’m new here so I might be wrong.
Thanks both for your input. BlueFragment, I definitely considered that, but there would be a lag time between the throttle decrement and when the ship’s velocity reached its new equilibrium. The result would be a slow series of steps which might look kinda weird, and be cumbersome to script.
I’ve rewritten the movement script so that I’m setting the velocity of the rigidbody direction rather than using force. Its easier to manage and as long as the values are lerped up and down, it looks realistic enough. Now I can simply set the other ships velocity as a target to be matched.
You can use ForceMode.VeloctiyChange, and use the target’s velocity minus the player’s velocity, and it will automatically set the correct force for whatever mass the player is.
Oh, thanks for the idea! So if I want to decelerate the player’s spacecraft over time to match the other player’s velocity I should maybe use the Acceleration ForceMode and keep applying it until they match?
(Or I guess, to use the VelocityChange, I could get the other ship’s speed, save that as a target, then slowly change the value given to AddForce until they match. I think the result is the same).