Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Yarden-Raveh · May 07, 2013 at 11:15 AM · rotationgameobjecttransformanglemathf.clamp

Limiting a GameObject Rotation

I have a GameObject i am trying to rotate. i do not want it to rotate beyond a certain angle. Mainly because i do not want it to complete a 360 degree rotation.

Here is the code i have been using:

     v=Input.GetAxis("VerticalMouse")*Time.deltaTime*sensetivity;
     rotationDegree=inverted*v;
     rotationDegree+=transform.rotation.x;
     Mathf.Clamp (rotationDegree,minimumRotate,maximumRotate);
     rotationDegree-=transform.rotation.x;
     transform.Rotate (rotationDegree,0,0);

Regardless of whatever value i enter to minimumRotate and maximumRotate the objects can still complte a 360 angle rotation.

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
1
Best Answer

Answer by aldonaletto · May 07, 2013 at 11:28 AM

You're incorrectly using rotation.x: rotation is a quaternion, and its XYZW components have nothing to do with the rotation angles - you should use transform.eulerAngles instead:

 v=Input.GetAxis("VerticalMouse")*Time.deltaTime*sensetivity;
 rotationDegree=inverted*v;
 var curX: float = transform.eulerAngles.x; // get the current X angle
 rotationDegree += curX;
 Mathf.Clamp (rotationDegree,minimumRotate,maximumRotate);
 rotationDegree -= curX;
 transform.Rotate (rotationDegree,0,0);

But be aware that reading eulerAngles may return weird values when there's some rotation about more than one angle - in this case, the rotation about axes Y and Z should be zero.

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 Yarden-Raveh · May 07, 2013 at 11:41 AM 0
Share

Thank you that was very helpful, also i upgraded the code to:

v=Input.GetAxis("Vertical$$anonymous$$ouse")*Time.deltaTime*sensetivity*inverted;

     rotationDegree+=v;
     rotationDegree=$$anonymous$$athf.Clamp (rotationDegree,$$anonymous$$imumRotate,maximumRotate);
     transform.localEulerAngles=new Vector3(rotationDegree,0,0);

seems a bit cleaner this way.

avatar image aldonaletto · May 07, 2013 at 11:55 AM 0
Share

Actually, this is the best way to do a well controlled rotation: "rotate" the angle mathematically and assign it to eulerAngles or localEulerAngles, as you're doing now. Always remember to set the 3 axes at once: assigning to one axis only is allowed in JS (not in C#), but may produce really weird results.

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

13 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

Related Questions

Animating in Unity Rotation snaps to last rotation key 0 Answers

Make an object move to a given point by rotating to the correct direction first. 1 Answer

How do I rotate the hook? 1 Answer

Rotate angle 0 Answers

Transform a gameobject y rotation to another gameobject y rotation 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