• 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 Sprakle · Jun 27, 2013 at 12:35 AM · rotationclamprelative

Clamp to relative roation

I have problem exactly like this: http://answers.unity3d.com/questions/363052/mathfclamp-rotation-relative-to-parentchild-object.html

I would just upvote it, but I don't have enough reputation, and I have a bit to add.

Here is my code so far, much of it taken from this post:

 private Vector3 ClampVector3(Vector3 relativeTo, Vector3 value, Vector3 maxDifference)
     {
         float xMin = relativeTo.x - maxDifference.x;
         float xMax = relativeTo.x + maxDifference.x;
         float yMin = relativeTo.y - maxDifference.y;
         float yMax = relativeTo.y + maxDifference.y;
         float zMin = relativeTo.z - maxDifference.z;
         float zMax = relativeTo.z + maxDifference.z;
 
         float x = ClampAngle(value.x, xMin, xMax);
         float y = ClampAngle(value.y, yMin, yMax);
         float z = ClampAngle(value.z, zMin, zMax);
 
         Vector3 clamped = new Vector3(x, y, z);
          return clamped;
     }
 
     public static float ClampAngle(float angle, float min, float max)
     {
         if (angle < 90 || angle > 270)
         {
             if (angle > 180) angle -= 360;
             if (max > 180) max -= 360;
             if (min > 180) min -= 360;
         }
         angle = Mathf.Clamp(angle, min, max);
         if (angle < 0) angle += 360;
         return angle;
     }

The main issue is with the ClampAngle() method, which is shown by aldonaletto's comment in the first answer of the second post I linked. How can this issue be resolved?

The comment:

Please notice that this function has a "capture" range: if the parameter angle is between -90 and +90 degrees around the center of the range, it will be clamped correctly; if its out of this range, the function may "think" it is at the "other side" of the circle, and clamp to the wrong limit - for example: if the range is centered at 120 degrees, an angle lower than 270 will be clamped to 160 degrees, but above 270 it will be clamped to 80 degrees. This will not be a problem if you're clamping the angle returned by eulerAngles after a small rotation (what seems to be your case), since it will not be too away from the range. If you want to clamp an arbitrary angle to the limits, however, this problem may arise.

Comment
Add comment · Show 3
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 robertbu · Jun 27, 2013 at 12:40 AM 0
Share

You don't include the code where you actually set the rotation. I just put an answer in the other question. I'm assu$$anonymous$$g you have the same problem. You want to use transform.localRotation or transform.localEulerAngles when you set the angle. Note it is best to treat the localRotation/localEulerAngles as write-only as was done in the other question. That is to maintain your own variables for the angles and not try to read the angles from the transform.

avatar image Sprakle robertbu · Jun 27, 2013 at 01:27 AM 0
Share

I should have mentioned the "head" of my code is not actually a child of the object restraining it, so that does not make a difference. To clear things up, here is an example of the ClampAngle() method working correctly:

 relativeTo = (0, 51.7, 0)
 value (aim) = (6.4, 69.2, 0)
 x$$anonymous$$in = -100, x$$anonymous$$ax = 100
 y$$anonymous$$in = -48,  y$$anonymous$$ax = 151
 z$$anonymous$$in = -100, z$$anonymous$$ax = 100
 clamped = (6.4, 69.2, 360)
 
 And debug output of it not working:
 relativeTo = (0, 51.7, 0)
 value (aim) = (11.2, 189.8, 0)
 x$$anonymous$$in = -100, x$$anonymous$$ax = 100
 y$$anonymous$$in = -48,  y$$anonymous$$ax = 151
 z$$anonymous$$in = -100, z$$anonymous$$ax = 100
 clamped = (11.2, 311.7, 0)
 `
avatar image robertbu · Jun 27, 2013 at 03:54 AM 0
Share

Your problem is different than the one in the question you reference. I've converted by answer to a comment so that other will look at this now unanswered question.

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.

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

15 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

Related Questions

Clamp gyro rotation 0 Answers

clamp rotation on rigidbody 1 Answer

Edited mouselook script rotation clamp not working 0 Answers

Rotation not observing limits 1 Answer

blocks being placed in an odd way 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