• 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 unitynewbie1 · Mar 13, 2012 at 04:25 AM · rotationquaternionlimitaiminggimbal-lock

Limit local rotation around x, y, and z axes?

I’m rotating a character's arm toward a target object. I have a maximum and a minimum rotation limit (for each of the x, y, and z directions) that I want the arm to stay within the range of.

I have tried a number of things that have all caused the arm to do a strange flipping behavior, which I think is resulting from gimbal lock. I have encountered this problem of gimbal lock when I tried:

-Setting the angle to the max limit when the attempted rotation exceeded that range. And if the angle of attempted rotation was between 180 and 360, I converted the number to a negative, and then set it to the minlimit if it was less than that:

 var minlimit=Vector3(-20,-20,-20);
 var maxlimit=Vector3(30,30,30);
 
 var negconvx=newQuat.eulerAngles.x;
 var negconvy=newQuat.eulerAngles.y;
 var negconvz=newQuat.eulerAngles.z;
 
 if(newQuat.eulerAngles.x>=180){
      negconvx=newQuat.eulerAngles.x-360;
 }
 if(newQuat.eulerAngles.y>=180){
      negconvy=newQuat.eulerAngles.y-360;
 }
 if(newQuat.eulerAngles.z>=180){
       negconvz=newQuat.eulerAngles.z-360;
 }
     
 if(negconvx<=minlimit.x){
     x=minlimit.x;
 }    
 if(negconvy<=minlimit.y){
     y=minlimit.y;
 }    
 if(negconvz<=minlimit.z){
     z=minlimit.x;
     
 if(newQuat.eulerAngles.x>maxlimit.x){
     x=maxlimit.x;
 }    
 if(newQuat.eulerAngles.y>maxlimit.y){
     y=maxlimit.y;
 }    
 if(newQuat.eulerAngles.z>maxlimit.z){
     z=maxlimit.z;
 

-using Mathf.Clamp to clamp the rotations between a maximum and a minimum for x, y, and z as listed in the second answer for this limitrotation post.

-converting the range of rotation as suggested in the answer to this limitlocalrotation post. As mentioned in the comments section to that answer, the solution only solved for clamping after a small rotation, but not for clamping an arbitrary angle to the limits

I was thinking that one way I could get around the strange flipping behavior that I’m noticing is to be able to know when the arm is rotating clockwise or counterclockwise. If the arm is moving clockwise and is rotated beyond the maxlimit, I would set it to maxlimit. And if the arm is moving counterclockwise and is rotated beyond its minlimit, I would set it to minlimit.

I tried the code that's listed here to determine clockwise and counterclockwise direction with using the dot product and then the cross product, but it didn't reliably give me 1 for clockwise and -1 for counterclockwise for each axis of rotation. This is what I tried based on their code, but didn't work:

 var trend=currentAngles[boneindex]-previousAngles[boneindex];
 var perp: Vector3 = Vector3.Cross(transform.forward, trend);
 var rotdirection: float = Vector3.Dot(perp, transform.up);
     if (rotdirection > 0.0) {
         //turning clockwise
         var dirNum=1.0;
     } else if (rotdirection< 0.0) {
         //turning counterclockwise
         dirNum=-1.0;
     } else {
         dirNum=0.0;
     }

How can I determine if the rotation of an object toward a target is going in the counterclockwise or clockwise direction around the x, y, and z axes?

If there's a better way to limit the rotation without gauging counterclockwise or clockwise, please do share.

Any help would be very much appreciated, as I've been trying to figure out this problem for quite some time, scouring the forum posts and doing little experiments. I'm sort of new to this, so if I'm doing anything wrong, I'm open to constructive feedback.

Comment
Add comment · Show 1
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 Carterryan1990 · Mar 06, 2019 at 10:19 PM 0
Share

Im curious if you ever solved this?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reverse rotations problems... 2 Answers

Avoiding Gimbal Lock 1 Answer

Quaternion rotation flips axises when upside down 0 Answers

unwanted rotation on the y and z axes 1 Answer

IMU Sensor and Quaternion 2 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