• 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 DCTShinobi · Mar 01, 2015 at 03:51 AM · movementphysicsvelocityspace

Physics: Auto-stopping a spaceship with reverse thrusters

I'm playing with a concept for a spaceship game with physics. I have three rigidbody objects attached with fixed joints: a hull, a main engine, and a reverse thruster. There is no drag or angular drag. There is no gravity. The main engine and reverse thruster both have a "Thrust" script attached which uses AddForce to propel the ship forward or backward.

My goal is to create an "auto-stop" function for the ship. Basically, whatever the ship's (hull's) velocity is, I want a script to manipulate the engines' thrusts so the ship reaches a standstill.

As a test, I've been using the Z-axis only. The following code runs from an Update()...

     void Stop ()
     {
         Vector3 velocity = ship.GetComponent<Rigidbody>().velocity;
         Vector3 heading = ship.gameObject.transform.forward;
 
         float vx = velocity.x;
         float vy = velocity.y;
         float vz = velocity.z;
         float hx = heading.x;
         float hy = heading.y;
         float hz = heading.z;
 
 
 
         if (velocity != Vector3.zero)
         {
             if (vz == 0)
             {
                 Debug.Log ("The ship is not moving on Z");
             }
             else if (hz > 0 && vz > 1)
             {
                 mainEngine.GetComponent<Thrust>().output = 0;
                 reverseThruster.GetComponent<Thrust>().output = reverseThruster.GetComponent<Thrust>().maxThrust;
             }
             else if (hz > 0 && vz > 0)
             {
                 mainEngine.GetComponent<Thrust>().output = 0;
                 reverseThruster.GetComponent<Thrust>().output = 1;
             }
             else if (hz > 0 && vz < -1)
             {
                 reverseThruster.GetComponent<Thrust>().output = 0;
                 mainEngine.GetComponent<Thrust>().output = mainEngine.GetComponent<Thrust>().maxThrust;
             }
             else if (hz > 0 && vz < 0)
             {
                 reverseThruster.GetComponent<Thrust>().output = 0;
                 mainEngine.GetComponent<Thrust>().output = 1;
             }
         }
         else
         {
             Debug.Log ("The ship is stopped");
 
             mainEngine.GetComponent<Thrust>().output = 0;
             reverseThruster.GetComponent<Thrust>().output = 0;
         }
     }


It seems to work decently, though the ship never actually come to a complete stop. It still moves at a thousandth of a unit every second or two.

Should I move the code to work from a FixedUpdate() instead? Do I need to kill the engines and add in a small delay between checks, and if so, how? Or would I be better off just zeroing out the velocity directly through the script when it gets so low?

Thank you so much!

Comment
Add comment · Show 2
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 meat5000 ♦ · Mar 01, 2015 at 04:22 AM 1
Share

Invert the velocity and greatly reduce its magnitude. Add the result to the velocity periodically.

avatar image DCTShinobi · Mar 01, 2015 at 06:31 AM 0
Share

Thanks. Yes, I was using the different engines to counteract each other when necessary. And I did end up having to lower their outputs when the ship got slower. It ended up working quite well for one axis.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by salex100m · Mar 01, 2015 at 05:28 AM

I don't think you can ever get to true ZERO velocity by adding and subtracting thrust. It would be much cleaner to just implement a fullStop() function that says:

if (currentVelocity < minimumV && currentVelocity > -minimumV ) then velocity = 0

basically... force a stop when you reach some minimum threshold.

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 DCTShinobi · Mar 01, 2015 at 06:28 AM 0
Share

Thank you. It may come to that for all three axes.

I actually got a full-stop on one axis by adding more $$anonymous$$ute velocity checks and lowering outputs to match.

I also moved it to FixedUpdate(), so that may have helped also.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

move 2d character affected by physics with velocity and/or add force 2 Answers

How to move a gameObj by cicking/ touching another gameobj? 0 Answers

How can you carry rigidbodies without them appearing jumpy? 1 Answer

Player slows down when jumping/velocity changes 1 Answer

Constraining maximum movement speed on a rigidbody on certain axes. 1 Answer


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