Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 IssaMeTiki · Jun 08, 2021 at 05:19 AM · quaternioneuleranglestransform.rotationmathf.clamptransform.rotate

How to use a negative value when MathF.Clamping a eulerAngle?

So I'm trying to get my platform to be able to rotate in between the values of -25 and 25 along both the X and Z axis, but whenever the x/z rotation value is <= 0 it resets back to 25.

This is my current code

         // Rotate Level Forward
         if (Input.GetKey(KeyCode.W))
         {
             gameObject.transform.Rotate(new Vector3(Time.deltaTime * xRotationSpeed, 0, 0));
         }

 
         // Rotate Level Left
         if (Input.GetKey(KeyCode.A))
         {
             gameObject.transform.Rotate(new Vector3(0, 0, Time.deltaTime * zRotationSpeed));
         }
         // Rotate Level Backwards
         if (Input.GetKey(KeyCode.S))
         {
             gameObject.transform.Rotate(new Vector3(-Time.deltaTime * xRotationSpeed, 0, 0));
         }
         // Rotate Level Right
         if (Input.GetKey(KeyCode.D))
         {
             gameObject.transform.Rotate(new Vector3(0, 0, -Time.deltaTime * zRotationSpeed));
         }
 
         _xClamp = Mathf.Clamp(transform.eulerAngles.x, xRotationCapNeg, xRotationCapPos);
         _zClamp = Mathf.Clamp(transform.eulerAngles.z, zRotationCapNeg, zRotationCapPos);
 
         gameObject.transform.eulerAngles = new Vector3(_xClamp, transform.rotation.y, _zClamp);

All within my ProcessInputs() function

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

Answer by Eno-Khaon · Jun 08, 2021 at 06:04 AM

Something to consider: Unity uses Quaternions for rotations, not Euler Angles/Degrees.

Those rotations, however, are then translated back into Euler Angles for your benefit, as they're easier for human comprehension. Likewise, you are generating a rotation, converted from Euler Angles into a Quaternion rotation, which is then converted back for your benefit.

With this in mind, it means that the rotation can be represented in a variety of ways when described in degrees. For example, 350 degrees and -10 degrees would be the same amount of rotation on a given axis, so that's what would be offered up in the conversion from Quaternion to Euler rotation.

One example of how you can work around this is to instead apply the rotation as an absolute relative to a given baseline (for example, no rotation would be Quaternion.identity, or Vector3.zero Euler):

 Vector2 currentRotation = Vector2.zero;
 Quaternion baseRotation = Quaternion.identity;
 
 // ...

 // Combined input for WASD all at once
 Vector2 rotationInput = new Vector2(Input.GetAxis("Horizontal") * zRotationSpeed, Input.GetAxis("Vertical") * xRotationSpeed);
 
 // Apply new Input rotation to the running total, then clamp the total
 currentRotation += rotationInput;
 currentRotation = new Vector2(Mathf.Clamp(currentRotation.z, zRotationCapNeg, zRotationCapPos), Mathf.Clamp(currentRotation.x, xRotationCapNeg, xRotationCapPos));

 // I always forget the order of operations on this and I'm not in a position to test right now. The order of multiplications here might need to be sorted.
 // (i.e. 6 possible combinations and relative orders of operations for non-commutative multiplication)
 transform.rotation = baseRotation * Quaternion.AngleAxis(currentRotation.x, Vector3.forward) * Quaternion.AngleAxis(currentRotation.z, Vector3.right);
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

121 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform.Rotate producing unexpected results when being used after setting localEulerAngles 0 Answers

Rotation around z-axis always local space and not world? Vector-axis angle projection calcuations. 0 Answers

How do I clamp the Z-Axis Rotation for this code? 1 Answer

Calculate sum over all movements and rotations of object 1 Answer

Limiting rotation based on direction 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