• 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 Tea_Doogun · Dec 12, 2012 at 07:56 PM · rotationmathf.clamp

Mathf.Clamp rotation relative to parent/child object

  • I have a 'head' object as a child to a 'body' object

  • There is a Mathf.Clamp on the child-head restricting the y rotation from -90 to 90 degrees

  • This clamp seems to be relative to world space because when i turn the body, the head will only turn in the same forward-facing -90 to 90 degree arc, whereas i'd like the -90 to 90 degrees arc to be relative to the child object itself

  • so which ever direction is 'zero degrees' on the y rotation, i want the -90 to 90 degree turning arc to be relative to that.

---

    var lookSensitivity : float = 5.0;
     var yRotation : float;
     var xRotation : float;
     var currentYRotation : float;
     var currentXRotation : float;
     var yRotationV : float;
     var xRotationV : float;
     var lookSmoothDamp : float = 0.1;
     
     function Update () {
     
         yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
         xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
         
         xRotation = Mathf.Clamp(xRotation, -90, 45);
         
         //THIS IS THE PART I WANT HELP WITH -- restricts side to side head movement
         yRotation = Mathf.Clamp(yRotation, -90, 90);
         
         
         currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
         currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, xRotationV, lookSmoothDamp);
     
         transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
         
     }

PLEASE inform me if my query is not explained well enough and i will re-think and re-post.

Thank you all, Tom :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Jun 27, 2013 at 12:34 AM

You want to use transform.localRotation instead of transform.rotation.

Comment
Add comment · Show 3 · 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 Tea_Doogun · Jun 27, 2013 at 07:21 AM 0
Share

Thanks, man :)

avatar image robertbu · Jun 27, 2013 at 09:05 AM 1
Share

The question is over a year old, but someone else made reference to it in their question and wanted an answer. I'm sure you figure out a solution a long time ago.

avatar image Tea_Doogun · Jun 27, 2013 at 10:17 AM 0
Share

Still nice to know that someone cares :)

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

12 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

Related Questions

eliminating angular velocity when clamp value hit? 0 Answers

How to make a gameobject have a rotation constraint? 1 Answer

Flip over an object (smooth transition) 3 Answers

Mathf.Clamp Negative min not negative? 1 Answer

Mathf.Clamp issues 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