Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Avanak · Apr 08, 2015 at 03:07 AM · movementspaceshipworld spaceinertialocal space

Help with rigidbody slowing (inertia dampening)

I am making a space game where the player controls a spaceship that moves according to newton's laws of motion. The spacecraft can translate forward/back, up/down and left/right. It can also pitch, yaw and roll.

Code for this:

     Rigidbody rb;
     
     public float thrustForward;
     public float thrustBack;
     public float thrustUp;
     public float thrustDown;
     public float thrustLeft;
     public float thrustRight;
     public float thrustPitch;
     public float thrustYaw;
     public float thrustRoll;
     public float mouseSteerSensitivity;
     
     float xThrust;
     float yThrust;
     float zThrust;
     float xRot;
     float yRot;
     float zRot;
     float mouseXSteer;
     float mouseYSteer;
     
     bool inertiaDampeners;

 //reset thrusters for this frame
         zThrust = 0;
         yThrust = 0;
         xThrust = 0;
         xRot = 0;
         yRot = 0;
         zRot = 0;
         
         //get the active thrusters
         if (Input.GetKey(KeyCode.W)) {zThrust = thrustForward;}
         else if (Input.GetKey(KeyCode.S)) {zThrust = -thrustBack;}
         
         if (Input.GetKey(KeyCode.R)) {yThrust = thrustUp;}
         else if (Input.GetKey(KeyCode.F)) {yThrust = -thrustDown;}
         
         if (Input.GetKey(KeyCode.D)) {xThrust = thrustRight;}
         else if (Input.GetKey(KeyCode.A)) {xThrust = -thrustLeft;}
         
         if (Input.GetKey(KeyCode.UpArrow)) {xRot = thrustPitch;}
         else if (Input.GetKey(KeyCode.DownArrow)) {xRot = -thrustPitch;}
         
         if (Input.GetKey(KeyCode.RightArrow)) {yRot = thrustYaw;}
         else if (Input.GetKey(KeyCode.LeftArrow)) {yRot = -thrustYaw;}
         
         if (Input.GetKey(KeyCode.Q)) {zRot = thrustRoll;}
         else if (Input.GetKey(KeyCode.E)) {zRot = -thrustRoll;}
         
         //get the amount of mouse steering
         mouseXSteer = Input.GetAxis("Mouse X");
         mouseYSteer = Input.GetAxis("Mouse Y");
 
         //acceleration
         rb.AddRelativeForce(xThrust, yThrust, zThrust);
         
         //rotation
         Vector3 rotation = new Vector3(xRot, yRot, zRot);
         rb.AddRelativeTorque(rotation);
         
         Vector3 mouseRotation = new Vector3(-mouseYSteer, mouseXSteer, 0);
         rb.AddRelativeTorque(mouseRotation * mouseSteerSensitivity);

Now I want to apply force in the negative moving direction to gradually slow down the ship when the inertia dampeners are active. It works when I am facing forwards in word space, but as soon as I turn the ship around, the ship starts moving in the wrong directions. This is weird as I use transform.InverseTransformDirection(rigidbody.velocity) to get the local moving direction.

The code for the inertia dampening:

         //check for inertiaDampeners toggle
         if (Input.GetKeyDown(KeyCode.LeftControl)) {
             inertiaDampeners = !inertiaDampeners;
         }
 
         //add inertia damener force
         if (inertiaDampeners == true) {
             Vector3 currentVel = transform.InverseTransformDirection(rb.velocity);
 
             if (currentVel.x > 0) {
                 rb.AddRelativeForce(transform.right * -thrustLeft);
             } else if (currentVel.x < 0) {
                 rb.AddRelativeForce(transform.right * thrustRight);
             }
             
             if (currentVel.y > 0) {
                 rb.AddRelativeForce(transform.up * -thrustDown);
             } else if (currentVel.y < 0) {
                 rb.AddRelativeForce(transform.up * thrustUp);
             }
             
             if (currentVel.z > 0) {
                 rb.AddRelativeForce(transform.forward * -thrustBack);
             } else if (currentVel.z < 0) {
                 rb.AddRelativeForce(transform.forward * thrustForward);
             }
         }


Does anyone know how to fix this? Or did I misunderstand transform.InverseTransformDirection(rigidbody.velocity).

Many thanks in advance!

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Rotate and transform in local space 2 Answers

Create variable based on local axis 1 Answer

How do I create a spaceship 1 Answer

Problem with bullet projectiles direction 2 Answers

Forward movement rotated on movement script? 0 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