- Home /
Simulating Fighter Jet Physics (with addForce () )
Hello, I'm trying to create a prototype of a jet fighter game. What I want to do is to control the thrust of the plane in such a way that I know exacly how long it will take to accelerate to a certain speed.
I know that I can control the velocity manually, but I prefer to stick to the built-in physics.
 public float acceleration = 40; // meters per second squared
 public float topSpeed = 1500; // km per hour - The terminal velocity of the plane??
 
 private float thrust = 0;
 
 void Update () {
     float speed = r.velocity.magnitude * 3.6f;
     print(speed + " km/h");
     if (Input.GetKey (KeyCode.Mouse0)) {
             thrust = 1; // if left mouse button held, then accelerate
         } else if (Input.GetKey(KeyCode.Mouse1)) {
             thrust = -2; // if right mouse button held, then deccelerate
         } else {
             thrust = 0; // if no mouse button pressed, don't accelerate
         }
 }
 
 void FixedUpdate () {
     r.AddRelativeForce(0, 0, acceleration * thrust, ForceMode.Acceleration);
 }
I've set drag to 1 to prevent the plane from accelerating forever. However, the top speed I can reach is 150 km/h. It also takes way more time that around 4 seconds (as my acceleration should make it).
How can I set the terminal velocity of the plane to something specific (eg 1,500 km/h) and the acceleration realistic?
Should I mess around with the drag and mass values? Is the value passed into addForce () something else, and not acceleration?
Code examples are welcome.

Answer by Rostam24 · Sep 26, 2015 at 10:51 PM
Here is my code. It's not as nice as it could be, but I think it has everything you want (even though it's just for a regular character in a 2d platformer):
 speed = rigidBody.velocity.x;
 var maxSpeed = currentMaxSpeed;    
 var acc = maxSpeed;
 if(!MovingRight)
     acc *= -1;
 if(!moving)
     acc = 0;        
 if(Mathf.Abs(speed) > maxSpeed)
     acc = 0;
 
 var dec = 4f;
 if(speed < 0)
     dec *= -1;
 if(Mathf.Abs(speed) < 1)
     dec = 0;
 
 rigidBody.AddForce(new Vector2(acc-dec, 0f), ForceMode2D.Impulse);
Answer by Wolfdog · Sep 28, 2015 at 07:12 AM
Thanks to @Rostam24 , I was able to put together a script for my jet. I'll post it here in case anyone needs something similar in the future.
 using UnityEngine;
 using System.Collections;
 
 public class fighterjetPhysicsControllerScript : MonoBehaviour {
 
     public Rigidbody r;
 
     public float rollRate = 70;
     public float pitchRate = 50;
 
 
     // let me input in KM/H format
     public float _topSpeed = 1500f; // km/h
     public float _myAccel = 50;     // how many KM/H my plane gains each second
     public float _startSpeed = 0;
 
     // use M/S format for the script
     private float topSpeed;
     private float acceleration;
     private float myAccel;
     private float startSpeed;
 
     private float speed;
 
     private float thrust = 0;  
 
     public float gravityConstant = 9;
 
     public Vector3 com = Vector3.zero;
 
     private float returnKmSpeed(float value) {
         return value * 3.6f; // convert from M/S to KM/H
     }
 
     private float returnMSpeed(float value) {
         return value * 0.27777777777778f;  // convert from KM/H to M/S
     }
 
     void Start() {
         r.centerOfMass = com; // set center of mass
 
         topSpeed = returnMSpeed(_topSpeed);
         myAccel = returnMSpeed(_myAccel);
         startSpeed = returnMSpeed(_startSpeed);
 
         r.AddRelativeForce(0, 0, startSpeed, ForceMode.VelocityChange); // add a start speed (eg if plane starts mission in air
     }
 
     void Update()
     {
         speed = r.velocity.magnitude; // get speed
         print(returnKmSpeed (speed) + " km/h"); // print speed in KM/H format
         if (Input.GetKey(KeyCode.Mouse0))
         {
             thrust = 1; // if left mouse button held, then accelerate
         }
         else if (Input.GetKey(KeyCode.Mouse1))
         {
             thrust = -2; // if right mouse button held, then deccelerate
         }
         else
         {
             thrust = 0; // if no mouse button pressed, don't accelerate
         }
 
         acceleration = Mathf.Clamp (acceleration + (myAccel * thrust * Time.deltaTime), 0, topSpeed); // make sure not to go faster than top speed
     }
 
     void FixedUpdate()
     {
         r.AddRelativeTorque(Input.GetAxis("Vertical") * pitchRate * Time.fixedDeltaTime, 0, -Input.GetAxis("Horizontal") * rollRate * Time.fixedDeltaTime, ForceMode.Acceleration); // pitch and yaw
 
         r.drag =  1 + (Mathf.Abs (thrust) / topSpeed); // some fancy physics to set a terminal velocity
         r.AddRelativeForce(0, 0, acceleration, ForceMode.Acceleration); // the actual physics for acceleration 
 
         r.AddForce(0, -gravityConstant, 0); // my own gravity (disable gravity on the rigidbody component
         r.AddRelativeForce(0, Mathf.Clamp ((speed / topSpeed) * gravityConstant * 4, 0, gravityConstant), 0); // add lift based on speed, unitl lift 
         // is the same as downward force (about 1/4 of top speed)
     }
 }
Thanks for this question and answer exactly what I needed just need a script now for firing :)
Your answer
 
 
             Follow this Question
Related Questions
Problem Trying to Apply Non-Kinematic Velocity to Rigidbody 0 Answers
GameObject disappears when velocity is set to (near) zero. 1 Answer
Unity relative velocity limiting 0 Answers
strange unrealisticly physic problems about velocity/gravity acceleration 2 Answers
RigidBody clips into corners 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                