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 pasza89 · Mar 31, 2014 at 10:38 PM · rotationrigidbodycareuleranglessimple

How to make car land on 4 wheels and maintain speed?

So I have made a simple physics for my car game. It is a top-down racer, similar to NES era games, here's a screenshot for you to get the general idea - LINK

You can drive the car forward/backwards with standard acceleration, but driving to the sides is like strafing, the car doesn't turn to the left/right and it doesnt rotate in these axes. The only axis which he can rotate is X, that means it can rotate forward/backward (like when driving uphill/downhill). I didn't want the car to rotate like crazy (too easily, too fast), so I wrote a script that stops the rotation to some extent:

 if (transform.eulerAngles.x >= 30 && transform.eulerAngles.x < 90 && !IsGrounded()){
     rigidbody.AddTorque (-7*Time.deltaTime, 0, 0);
     }
 if (transform.eulerAngles.x > 270 && transform.eulerAngles.x < 330 && !IsGrounded()){
     rigidbody.AddTorque (7*Time.deltaTime, 0, 0);
     }


Visualization if you don't understand what happens - number 2 and 4 - CLICK

It works fine, except for when the car hits something with high speed when it is rotated (number 3 and 5 on picture above) - the force that gets applied to the rigidbody during collision is so high that my script doesn't stop it (because it adds just a moderate amount of force - otherwise it makes things even worse). Yet another picture: CLICK

Even though I blocked the car entity from rotating in 2 axes, I can't do it in third, because then it would be impossible to drive uphill/downhill

I wanted to know if there is a way to make the car land "gently". I don't want to use preset rotation, resetting Quaternion or force values, because that makes the car "jump" to that setting in 1 frame. If I add yet another limiter similar to the script above, it is band-aid solution, because if force is strong enough it will overcome the script. I tried adding manual rotation control mid-flight, but it doesn't stop the player from rotating the car into the "flipping" angle. If I limit the rotation by a script (sorry for pseudo-code, but I can't find it):

 if(rotation > 60){
     rotation = 60;
 }

It often makes the car stuck in this angle. I thought a solution might be something like this - CLICK

but I have no idea how to implement box colliders with constrained global rotation attached to the car, and I don't even know if that would work

I just came up with that script, which seems to solve the problem in a lot of cases, but still if the force is too big it seems to either fail or make car lose forward velocity entirely (which I want to keep - car drives at speed 20, falls down, lands with angle 55 degrees rotated forward, continues at speed 20 or slightly less - a picture of it - CLICK ):

           if (transform.eulerAngles.x >= 30 && transform.eulerAngles.x < 90 && !IsGrounded()){
               if(transform.eulerAngles.x >= 60 && transform.eulerAngles.x < 90){
                   transform.eulerAngles = Vector3(59.0, 0.0, 0.0);
                   rigidbody.angularVelocity = Vector3.zero;
                    rigidbody.AddTorque (-10*Time.deltaTime, 0, 0);
                    Debug.Log("301");
                   }
                rigidbody.AddTorque (-7*Time.deltaTime, 0, 0);
             }
         if (transform.eulerAngles.x > 270 && transform.eulerAngles.x < 330 && !IsGrounded()){
             if(transform.eulerAngles.x <= 300 && transform.eulerAngles.x > 270){
                 transform.eulerAngles = Vector3(301.0, 0.0, 0.0);
                 rigidbody.angularVelocity = Vector3.zero;
                    rigidbody.AddTorque (-10*Time.deltaTime, 0, 0);
                    Debug.Log("301");
                 }
                rigidbody.AddTorque (7*Time.deltaTime, 0, 0);
             }

I am open to suggestions, I've started with unity 4 days ago and I still don't know some basic stuff, forgive me if it has been answered (probably didn't know the right keywords). Thanks for reading :)

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

20 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

Related Questions

Tracking Rotation for Flips/rolls 3 Answers

Alternative to Quaternion.LookRotation() ? 2 Answers

EulerAngles.x stops at 90??? 2 Answers

How to get rigidbody local z axis rotation in Euler angles? 0 Answers

My rigidbody's y rotation is always 180 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