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
0
Question by THEpancakes · Oct 27, 2013 at 07:13 PM · rotationeuleranglesclamp

Constrained rotation problem

Ok, so I have a flat board that can be rotated on the X and Z axis by the arrow keys, but can't be rotated more than 10 degrees either way.

I came up with this code:

 private var newX : float;
 private var newZ : float;
 private var rot : Vector3;
 
 function Update () {
     rot = transform.eulerAngles;
     newX = Mathf.Clamp(rot.x+Input.GetAxis("Vertical"),-10,10);
     newZ = Mathf.Clamp(rot.z+Input.GetAxis("Horizontal"),-10,10);
     transform.eulerAngles = Vector3(newX,0,newZ);
 }

However I'm experiencing two problems:

  1. Let's just pretend for the moment I'm not doing anything on the X axis, and I'm only trying to rotate along Z. Rotation along Z works fine as long as long as it's a positive number, but as soon as it goes below 0 (and I want it to go all the way down to -10), the board just goes crazy and starts moving uncontrollably until it's back in positives...

  2. Along the X axis, nothing works at all. It just shakes uncontrollably no matter what the angle is.

I tested both these problems separatly (removing some code to test just one axis at a time), and I can't understand why it's doing this... Any ideas?

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

2 Replies

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

Answer by MasterTim · Oct 27, 2013 at 07:25 PM

 private var newX : float;
 private var newZ : float;
 private var rot : Vector3;
  
 function Update () {
     rot = transform.localEulerAngles;
     newX += Input.GetAxis("Vertical");
     newX = Mathf.Clamp(rot.x,-10,10);
     newZ += Input.GetAxis("Horizontal");
     newZ = Mathf.Clamp(rot.z,-10,10);
     transform.localEulerAngles = new Vector3(newX,0,newZ);
 }

try this

Comment
Add comment · 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 THEpancakes · Oct 27, 2013 at 07:41 PM 0
Share

That works great, cheers! (Had to replace "rot.x" and "rot.z" with "newX" and "newZ" but I figured that out quickly). Thanks for your help!

avatar image
1

Answer by robertbu · Oct 27, 2013 at 07:39 PM

Your problem is that you are reading eulerAngles. Unity stores rotations internally as Quaternions and translates them back into eulerAngle representations. There are multiple eulerAngle representations for any given physical rotation, so you are not necessarily dealing with the angles you think you are. The easiest fix for the code above is to treat eulerAngles as write-only. Assuming your object starts with no rotation (0,0,0), this should work:

     private var rot : Vector3 = Vector3.zero;
      
     function Update () {
         
         rot.x += Input.GetAxis("Vertical");
         rot.x = Mathf.Clamp(rot.x,-10,10);
         rot.z += Input.GetAxis("Horizontal");
         rot.z = Mathf.Clamp(rot.z,-10,10);
         transform.localEulerAngles = rot;
 }

There is also an issue with your original code where you are clamping your rotation before you are setting the final rotation.

Comment
Add comment · 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 THEpancakes · Oct 27, 2013 at 07:44 PM 0
Share

This works great just like the code from $$anonymous$$asterTim, thanks! :) Unfortunately I can't mark two answers as correct but I upvoted yours.

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

16 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

Related Questions

Bizzare problem with limiting camera veritcal rotation 2 Answers

Limit gameobject rotation to -480 and 480 degrees? 2 Answers

Clamping a wrapping rotation. 6 Answers

Edited mouselook script rotation clamp not working 0 Answers

Rotation not observing limits 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