• 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
1
Question by oliver344 · Jul 10, 2011 at 09:07 PM · rotationangleeulerangleslocallimit

Limit local rotation

Hi,

i have an issue i am not able to resolve myself. I have looked around topics here, but i couldnt find exactly what i am looking for.

I have an object, which is rotating around its axis, i.e. its orbiting on place. But i need to limit the rotation when it stops. So it means the object can have local y rotation anything between 0 and 360. Lets say its current y rotation is 100. I need to limit in +/- 40 interval. I.e. 60 - 140. Thats not problem, i am clamping it using eulerAngle.y. But when the current rotation is for example 20, then the issue comes up as it the interval is -20 to 60. Which causes issues as the rotation is only 0 - 360. So when the current rotation gets to this break point, whole limit code brakes and it does some funky stuff. Is there anyway how to prevent it from going in 0-360 interval and have negative values for rotation ? Or any other solution ?

Thanks a lot for any reply.

Luke

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 Waz · Jul 10, 2011 at 09:16 PM 0
Share

This is really just a coding question, so you should post your code.

avatar image mikii123 · Jun 15, 2016 at 06:31 PM 0
Share

Hi! Good news everyone! I'm using Unity 5.3.4 and there's no need for any angle conversion or special clamping. Quaternion.Euler() takes care of it. And it understands negative angles. So you just say $$anonymous$$athf.Clamp(angle, $$anonymous$$, max) and you're done. Have a nice day!

6 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by immerhart · Oct 19, 2012 at 07:07 PM

its a little late, but here my approach:

   m_startRotation = transform.rotation.eulerAngles.y;
   m_minRot = m_startRotation - 90.0f;
   m_maxRot = m_startRotation + 90.0f;
   m_isTranslationNessecary = false;
 
   // check if rotation happens between 0/360
   if(m_minRot <= 0.0f)
    m_minRot += 360.0f;
 
   if(m_maxRot >= 360.0f)
    m_maxRot -= 360.0f;
 
   if(m_minRot > m_maxRot)
    m_isTranslationNessecary = true;

and this function checks if the obj can be rotated:

     private float ClampAngle(float angle, float min, float max, bool isTranslationNecessary)
     {
         if(!isTranslationNecessary)
         {
             if(angle < min )
                 return min;
 
             if(angle > max)
                 return max;
         }
         else
         {
             if(angle > max && angle < min)
             {
                 if(min - angle > angle - max)
                     return max;
                 else
                     return min;
             }
         }
 
         return angle;
     }
Comment
Add comment · 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
  • ‹
  • 1
  • 2

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

18 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

Related Questions

Limit local Rotations on one axis 3 Answers

Rotation Limit 2 Answers

Always rotating an object to a set angle? 0 Answers

How do I clamp the Z-Axis Rotation for this code? 1 Answer

limiting rotation to solid angle 5 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