Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Constraining rotation to set min/max issues

Discussion in 'Scripting' started by SUBZERO8K, Mar 23, 2016.

  1. SUBZERO8K

    SUBZERO8K

    Joined:
    Jan 15, 2013
    Posts:
    36
    Hi,

    I am trying to rotate a cylinder that is attached to my player using the 1/2 keys. I want this rotation to only move on the X axis. To do this I am currently doing the following within Update(), which partially works:

    Code (CSharp):
    1.  
    2.         if (Input.GetKey("1"))
    3.         {
    4.             javelinLauncher.transform.Rotate(new Vector3(-1, 0, 0) * Time.deltaTime * rotationSpeed);
    5.         }
    6.         if (Input.GetKey("2"))
    7.         {
    8.             javelinLauncher.transform.Rotate(new Vector3(1, 0, 0) * Time.deltaTime * rotationSpeed);
    9.         }
    10.  
    11.         if (javelinLauncher.transform.rotation.x > maxRotation)
    12.         {
    13.             javelinLauncher.transform.localRotation = Quaternion.Euler(30, 0, 0);
    14.         }
    15.         if (javelinLauncher.transform.rotation.x < minRotation)
    16.         {
    17.             javelinLauncher.transform.localRotation = Quaternion.Euler(0, 0, 0);
    18.         }
    This mostly constrains the X axis within the range of 0 to 30. However, I encounter some issues once I begin moving my player and performing this rotation. When I move in certain directions the rotation no longer stays within 0 and 30, but instead can go over 30 or snap from ~30 to 0. Another thing I noticed is that the rotation always seems to go slightly over 30 before being constrained back to 30, which causes it to look a bit jittery when the rotation is being applied and its already at/near its max rotation. I am not sure what could be causing these issues, any advice on different approaches to try?

    Some random side notes that may be of importance:
    - The Game Object I am rotating has a scale of (0.4, 0.5, 0.4)
    - Before changing the players direction, constraining the rotation always works.

    Here is a link to a GIF showing the issue:
    http://i.imgur.com/1OoTHTe.gifv

    Thanks for your time!
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    It's probably because you're trying to access world-space quaternion rotation values in your if statements. I don't know what you have for minRotation and maxRotation, but if they are 0 and 30, then this won't work the way you want it to. It doesn't really makes sense to set local Eulers based on world quaternion values, right?

    Try checking against transform.localRotation.eulerAngles.x instead. This way you'll be looking at the x-rotation relative to the parent and about the x axis in degrees.
     
  3. SUBZERO8K

    SUBZERO8K

    Joined:
    Jan 15, 2013
    Posts:
    36
    Thanks, just implemented this change and it seems to have fixed the abnormalities occurring during movement and the Game Object now locks perfectly at 30. However the same doesn't seem true for the minimum, when it reaches 0 the GameObject always snaps back to an X rotation value of 30, even though it should be getting set to 0.

    Code (CSharp):
    1.         if (javelinLauncher.transform.localRotation.eulerAngles.x > maxRotation)
    2.         {
    3.             javelinLauncher.transform.localRotation = Quaternion.Euler(maxRotation, 0, 0);
    4.         }
    5.         if (javelinLauncher.transform.localRotation.eulerAngles.x < minRotation)
    6.         {
    7.             javelinLauncher.transform.localRotation = Quaternion.Euler(minRotation, 0, 0);
    8.         }
    My min and max are set to 0 and 30, respectively.
     
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Euler angles wrap from 0 to 360! You'll need to change your logic to account for this. Try checking for an angle greater than 300 and less than 360 or something like that.
     
    SUBZERO8K likes this.
  5. SUBZERO8K

    SUBZERO8K

    Joined:
    Jan 15, 2013
    Posts:
    36
    Looks like it works, thanks!