Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 mheyman89 · Dec 21, 2020 at 05:06 AM · c#rotationmatheuleranglesconstraints

Limit Rotation Issues

Hello! So I created a simple script that rotates an object around the y axis when you hold down the "A" or "D" keys on the keyboard. With "A" going one direction and "D" going another. I have tried multiple ways to limit the rotation range. My desired range is 0-180 degrees. However nothing I do seems to work.


In this latest attempt I use a clamp to try and clamp the limits. I believe I have figured out what is going on but I don't know how to fix it. When my rotation exceeds 180 it sometimes becomes a negative number. And when my rotation goes under 0, instead of becoming a negative number, it becomes positive!


For example, if I rotate negatively towards 0, it will actually become a big number and then jump to the max side of the clamp: 180. There seems to be some weird stuff that happens when you hit 0 and 180. I tried to mitigate this by clamping between 1 - 179. This seemed to work! However if I rotate by anything other than 1 degree the issue comes back because it is not evenly landing on 1 and 179.


Does anyone know how I can fix this. I just want a clean limit of 0-180, and I want to be able to rotate by any amount I want and have it stop when it hits that number! Maybe I need a lerp? Or a time function?

Here is my code for reference:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class rotator : MonoBehaviour
 {
     private float yRotation;
     private float rotationSpeed;
     private Vector3 currentRotation;
 
 
     // Start is called before the first frame update
     void Start()
     {
         yRotation = this.transform.localEulerAngles.y;
         rotationSpeed = 1.0f;
 
 }
 
     // Update is called once per frame
     void Update()
     {
 
         //Holding down "D" key.
         if (Input.GetKey(KeyCode.D))
         {
            
                 this.transform.localEulerAngles -= new Vector3(0, rotationSpeed, 0);
             
         }
 
         //Holding down "A" key.
         else if (Input.GetKey(KeyCode.A))
         {
         
                 this.transform.localEulerAngles += new Vector3(0, rotationSpeed, 0);
             
 
         }
         currentRotation = transform.localRotation.eulerAngles;
         currentRotation.y = Mathf.Clamp(currentRotation.y, 1f, 179.0f);
         transform.localRotation = Quaternion.Euler(currentRotation);
     }
 }
 
 
 





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 Eno-Khaon · Dec 21, 2020 at 05:42 AM

Rotations in Unity are performed using Quaternions and can then be reconstructed into Euler angle approximations thereafter. Being able to set the rotation using them, while convenient, isn't inherently the best approach to doing so.

With that in mind, you can replace the rotation scheme you're currently using with one that focuses on Quaternion.AngleAxis() instead.

 public float rotationSpeed = 90f; // ex. 90 degrees per second
 
 void Update()
 {
     if(Input.GetKey(KeyCode.A))
     {
         // Counter-clockwise example
         yRotation -= Time.deltaTime * rotationSpeed;
     }
     if(Input.GetKey(KeyCode.D))
     {
         // Clockwise example
         yRotation += Time.deltaTime * rotationSpeed;
     }
     yRotation = Mathf.Clamp(yRotation, 0f, 180f);
     transform.rotation = Quaternion.AngleAxis(yRotation, Vector3.up);
 }
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 mheyman89 · Dec 21, 2020 at 05:48 AM 0
Share

This worked perfectly! I tried so many things but Quaternion.AngleAxis was not one of them! Thank you so much for the help!

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

160 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 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

Flip over an object (smooth transition) 3 Answers

Use a single rotation axis of a freely rotating object 1 Answer

Transition between rotations 0 Answers

Copy rotation of another object with weight variable 2 Answers

Spiral galaxy math 3 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