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
0
Question by storaGeUnity · Aug 19, 2016 at 09:40 AM · c#physicsrigidbodyeuleranglesaddtorque

If angle is greater than 5, do something, if less than -5, do something else [Answered]

Hello!

I'm currently making a (loosely) physics based top-down car game that works pretty well so far.

One thing that I do have a problem though, is that when you steer either left or right, your car either rotates left or right, simulating car suspension.

Now, I've quickly created a little piece of code that doesn't work at all, and I'm not completely sure why (it's nearly 2am here so I am quite tired)

It's this:

         if ((Input.GetKey ("a")) & Car.rotation.eulerAngles.z <= -5){
             RigidCar.AddTorque(Car.forward * -100);
         }
 
         if ((Input.GetKey ("d")) & Car.rotation.eulerAngles.z >= 5){
             RigidCar.AddTorque(Car.forward * 100);
         }

I'd like all the help I can receive! Thanks!

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 Rajeesh_AR · Aug 19, 2016 at 11:23 AM 2
Share

It will not work for ((Input.Get$$anonymous$$ey ("a")) & Car.rotation.eulerAngles.z

Did you check whether it works for ((Input.Get$$anonymous$$ey ("d")) & Car.rotation.eulerAngles.z >= 5) ?? It seems that, the if statement is okay. But check whether the rotation angle is changing or not..

avatar image meat5000 ♦ · Aug 19, 2016 at 01:03 PM 0
Share

Are 'a' and 'd' set up in the Input $$anonymous$$anager? If not try using the $$anonymous$$eyCodes for the keys ins$$anonymous$$d.

 (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A))


Oh yes, the AND comparator consists of two ampersands not one

& should be &&

You second condition should be wrapped in brackets.

 if((Input.Get$$anonymous$$ey ("a")) && (Car.rotation.eulerAngles.z <= -5))

https://msdn.microsoft.com/en-us/library/sbf85k1c.aspx

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Trickman · Aug 19, 2016 at 12:11 PM

I feel you. I had this issue a few days ago.

Unity's "eulerAngles" tends to use positive angles more often than not, so when you're doing -5º, the engine is actually working with 355º. Because you compare them with regular >, < or == operators, they get compared as mere numbers, not angles.

I made a fast, recursive function to solve this problem. It's static, so you can put it in a "Utility.cs" script with a static class, so you can use it anywhere in your program (ie: Utility.ToEulerAngles(Car.rotation.eulerAngles.z);):

     /**
      * Returns a number expressed as euler angles with +-180 notation:
      * 
      * 500 -> 140
      * 450 -> 90
      * 270 -> -90
      * 0 -> 0
      * 180 -> 180
      * -180 -> 180
      */
     public static float ToEulerAngles(float number) {
         // POSITIVE NUMBERS
         if (number >= 0) {
             // [0, 180] -> Return number
             if (number >= 0 && number <= 180) {
                 return number;
             } 
 
             // (180, 360] -> Return number - 360
             else if (number > 180 && number <= 360) {
                 return number - 360;
             } 
 
             // (360, inf) -> Recursive calculation with number % 360
             else { /* if (number > 360) */
                 return ToEulerAngles (number % 360);
             }
         }
 
         // NEGATIVE NUMBERS
         else {
             // (0, -180) -> Return number
             if (number < 0 && number >= -180) {
                 return number;
             }
 
             // -180 -> 180
             else if (number == -180) {
                 return 180;
             }
 
             // (180, 360] -> Return number + 360
             else if (number < -180 && number >= -360) {
                 return number + 360;
             }
 
             // (-inf, -360) -> Recursive calculation with number % 360
             else { /* if (number < 360) */
                 return ToEulerAngles (number % 360);
             }
         }
     }

Hope it helps!

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
0

Answer by ScaniX · Aug 19, 2016 at 12:32 PM

Or just...

 Mathf.Repeat(n + 180, 360) - 180
Comment
Add comment · Show 2 · 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 storaGeUnity · Aug 19, 2016 at 01:16 PM 0
Share

That worked very well! Thanks, man!

avatar image meat5000 ♦ storaGeUnity · Aug 19, 2016 at 02:33 PM 0
Share

If its concluded, accept an answer

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

7 People are following this question.

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

Related Questions

AddTorque stops working when model is vertical 0 Answers

How to make Rigidbody.AddForce less delayed in Unity3D? 0 Answers

How does the RigidBody.Transform.rotate method work with degrees at 0? 0 Answers

How to make a dynamic bow shot and the arrow to curve and turn correctly when shot? 0 Answers

Unexpected "Jumping" Behavior 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