Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by SeriousTangents · Jan 04, 2016 at 02:47 PM · quaternionrotation axiseulerlimits

Trying to MathF.Clamp y axis rotation results in x equal 270 and y equal max value instantly

I am in the process of prototyping several Amusement Park style rides for an upcoming relaxation program. One of the rides is a rotating airplane ride where the individual arms will go up and down with a maximum rotational value of, say, zero to 45 degrees.

This is what I'm using for my rotational scripts:

     void Update () {
     
         if(rotateMe)    {
 
             this.gameObject.transform.Rotate((X_RotateSpeed * Time.deltaTime), (Y_RotateSpeed * Time.deltaTime), (Z_RotateSpeed * Time.deltaTime), Space.Self);
 
             tempAngle = transform.rotation.eulerAngles;
 
                 if (limitX)
                 {
                     angle = tempAngle.x;
                     angle = ClampAngle(angle, lowLimitX, highLimitX);
                     tempAngle.x = angle;
                     transform.rotation = Quaternion.Euler(tempAngle);
                 }
                 if (limitY)
                 {
                     angle = tempAngle.y;
                     angle = ClampAngle(angle, lowLimitY, highLimitY);
                     tempAngle.y = angle;
                     transform.rotation = Quaternion.Euler(tempAngle);
                 }
                 if (limitZ)
                 {
                     angle = tempAngle.z;
                     angle = ClampAngle(angle, lowLimitZ, highLimitZ);
                     tempAngle.z = angle;
                     transform.rotation = Quaternion.Euler(tempAngle);
                 }
 
         }  // end if rotateMe
 
     }  // end Update
 
     private float ClampAngle(float cAngle, float min, float max)    {
 
         if (cAngle < -360.0)
             cAngle += 360;
         else if (cAngle > 360.0)
             cAngle -= 360;
 
         cAngle = Mathf.Clamp (cAngle, min, max);
 
         if (min == max)
             return cAngle;
 
         if (cAngle == min || cAngle == max)
         {
             atLimit = true;
         }
 
         else
             atLimit = false;
 
         return cAngle;
     }
 

The central column only has a Z rotation speed, and with no limits selected it rotates as expected on the Z axis.

If I set an arm to have a Y rotation speed, the arm slowly lifts an continues to rotate a full 360. The arm does not dislocate, maintaining position as it rotates on the 360.

However, we don't want to dump out the passenger. So I tried to set up a clamp with a zero-45 limitation and then re-assigning the value back to the rotation. In the Screen Shots, the blue plane is the one with the assignments.

Setting LimitY to true, as soon as I start the simulation in the Editor, my x-value is automatically set to 270 my y-value is set to 45 (or whatever the maximum is) IF I assign a low value to the rotation speed (like 5) but if I set a very high rotation speed (like 50), I observe my z-value set to 270, my y-value set to the max y and my x-value will increment until it hits 90. In both cases, once it hit's the 'assigned' x value (either 270 or 90) the airplane will no longer move up or down, but maintain it's facing and slowly move in a circle, with the arm acting like a piston on a train. Also, the actual Y-rotation does not appear to take place. The Arm itself seems to have rotated not quite 90 degrees clockwise from initial position of the arm, and then will rotate to keep the blue plane as close to that point as possible.

I have to assume the fault is within my ClampAngle function, but I've used the code I found in another forum to address having an object rotate with limitations on a specific axis, so I'm not sure what I've entered in incorrectly.

Any advice would be appreciated.

Joseph.

atrest.png (391.1 kB)
limiteronlowspeed.png (377.2 kB)
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
0
Best Answer

Answer by SeriousTangents · Jan 07, 2016 at 05:10 PM

I was wrong about the fault being in the ClampAngle function. It was caused by -where- I was pulling the EulerAngles from.

There is an option to select gameObject.Transform.localEulerAngles and, once I pulled from there, my rotations were spot on.

There was an issue when using negative rotations starting from zero, or when a negative rotation moved an object to zero. It would bounce back to the 45 degree point, and I realized what was happening there is when the rotational reaches zero, it switches to 360 and therefore is clamped above the max to the 45 degree max. This was fixed by setting the minimum to 1 (on a backward rotation) and 359 (on a forward rotation) and making sure the arms/wrists had a starting value of 1 and 359 respectively.

             if (limitY)
             {
                 tempAngle = this.gameObject.transform.localEulerAngles;
                 angle = tempAngle.y;
                 angle = ClampAngle(angle, lowLimitY, highLimitY);
                 tempAngle.y = angle;
                 transform.localEulerAngles = tempAngle;
             }
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Quaternion/World Local rotation problem 0 Answers

Strange 2D Physics Behavior 0 Answers

How to test for rotation? 0 Answers

Assign an object rotation along a single axis from other object's rotation. 0 Answers

Keydoor problem 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