• 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
Question by JaredHD · Oct 29, 2017 at 07:17 AM · rotationeuleranglesmathf

Get an eular angle between 0 and 360 after calculations

Summary of my issue

Hello, I believe I am making a mistake. What I am trying to do is limit a rotation of an object in either two directions. Here is some code:

         if (startTower.transform.eulerAngles.y > 90 && startTower.transform.eulerAngles.y < 270)
         {
             Debug.Log(startTower.transform.eulerAngles.y + 90);
         }
         else if (startTower.transform.eulerAngles.y < 60 || startTower.transform.eulerAngles.y > 300)
         {
             Debug.Log(startTower.transform.eulerAngles.y + 90);
         }


The issue

Now the issue with the above, is that at 360, it adds 90. so it's 450 and I want 450-360, = 90. So I want 90. So is there a way to convert 450 into 90 while doing the calculation?


Or more specifically, is there a way to always get the degrees between 0 and 360, even if I add 90?


Note: You don't need to do this, I'll accept the answer if the above is right. So, if you are feeling generous, from the picture below, I want the wall to only appear on those two sides, where the blue arrows are. So left of the wall, or right of the wall.

So my plan is to replace the debug.log, with Mathf.Clamp, and give it 60 degrees leeway. If you would like to give a better solution than my idea, I am willing to try it.


End of generosity

Any help would be greatly appreciated

alt text

wall.jpg (53.5 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by haruna9x · Oct 29, 2017 at 08:51 AM

Try it:

     public static float Clamp0360(float eulerAngles)
     {
         float result = eulerAngles - Mathf.CeilToInt(eulerAngles / 360f) * 360f;
         if (result < 0)
         {
             result += 360f;
         }
         return result;
     }
Comment
nihohit1
BernardDtc
ILonion
wlwl2
insoluzioni
PowerCore2000
siddharth3322

People who like this

7 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 JaredHD · Oct 29, 2017 at 09:01 AM 0
Share

Yup, that gives me the answer I'm looking for, thanks

avatar image

Answer by unity_dS85HyjyiWEwcQ · Mar 28, 2022 at 09:02 AM

It would be quicker and more concise to just use the remainder operator.

 eulerAngles %= 360;


Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

102 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 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 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 avatar image avatar image avatar image

Related Questions

Rotation Limit 2 Answers

Find the difference between the Z rotation of two GameObjects 2 Answers

Mathf.SmoothDamp freeking out? 0 Answers

Rotate object towards joystick input using c# 1 Answer

Clamp Rotation Problem 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