Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Wolfdog · Sep 26, 2015 at 09:50 PM · c#physicsrigidbodyvelocityacceleration

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.

alt text

su-27-screenshot.png (262.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfdog · Sep 28, 2015 at 07:09 AM 0
Share

Thanks, I based my script on your one.

avatar image
2

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)
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Oshnoritsu · Sep 29, 2015 at 08:25 AM 0
Share

Thanks for this question and answer exactly what I needed just need a script now for firing :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges