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

Trying to limit a cube's rotation

Discussion in 'Scripting' started by Pedro_R, Apr 5, 2019.

  1. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    Hello,

    I'm trying to rotate a cube when I drag the mouse while holding down it's right button and limit the cube's rotation as it follows: X=[-30,30] ; X=[-180,180]
    The problem is that whenever it's X rotation value goes from 30 to 0 it suddently goes back to 30, and whenever it's Y rotation value goes from 180 to 0 it suddently goes back to 180. I would like the X to go all the way to -30 and staying this way until I drag the mouse to the other way, and the Y to go to -180.

    What am I doing wrong? Also, is there anything I could do in a better way?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CubeControl : MonoBehaviour
    6. {
    7.  
    8.     //Some variables.
    9.  
    10.     [SerializeField] float verticalSensibility = 5;
    11.     [SerializeField] float horizontalSensibility = 5;
    12.     [SerializeField] [Range(0, 50)] float maxHorizontalRotation = 30;
    13.     [SerializeField] [Range(-50, 0)] float minHorizontalRotation = -30;
    14.     [SerializeField] [Range(0, 180)] float maxVerticalRotation = 180;
    15.     [SerializeField] [Range(-180, 0)] float minVerticalRotation = -180;
    16.     Vector3 currentRotation;
    17.  
    18.     //Some code.
    19.  
    20.     void Update()
    21.     {
    22.         if (Input.GetMouseButton(1))
    23.         {
    24.             float horizontalRotation = horizontalSensibility * Input.GetAxis("Mouse X") * Time.deltaTime;
    25.             float verticalRotation = verticalSensibility * Input.GetAxis("Mouse Y") * Time.deltaTime;
    26.             gameObject.transform.Rotate(verticalRotation, horizontalRotation, 0, Space.World);
    27.             currentRotation = transform.localRotation.eulerAngles;
    28.             currentRotation.x = Mathf.Clamp(currentRotation.x, minHorizontalRotation, maxHorizontalRotation);
    29.             currentRotation.y = Mathf.Clamp(currentRotation.y, minVerticalRotation, maxVerticalRotation);
    30.             transform.localRotation = Quaternion.Euler(currentRotation);
    31.         }
    32.     }
    33. }
    34.  
    Thanks!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Does transform.Rotate and transform.localRotation references to the same GameObject?
     
    Last edited: Apr 5, 2019
  3. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    I think so, as the script is a component of the cube, but I'll try to change a little bit the reference to see if it works in some hours.
     
  4. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    It didn't work, I don't understand why.
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Clamping from eulerAngles is virtually impossible because of the nature of Quaternions and the way certain orientations can be expressed by multiple "different" rotations (90,0,90 and -270,180,-90 are the same for example).

    Your best bet, since you have a fixed range, is to store your pitch and yaw separately and operate on those values. Then rebuild the rotation every frame:
    Code (csharp):
    1.  
    2. private float pitch;
    3. private float yaw;
    4.  
    5. private void Update()
    6. {
    7.     pitch = Mathf.Clamp(pitch + your delta values, -45f, 45f);
    8.     yaw = Mathf.Clamp(yaw + your delta values, -45f, 45f);
    9.  
    10.     transform.localRotation = Quaternion.Euler(pitch, yaw, 0f);
    11. }
    12.  
     
  6. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    What do you mean by "Your delta values"? And why +-45f?
    Thanks!
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    I think is just copied example. So it could be as well +-30. The delta value, is the change, by how much you need rotate, from the base rotation of pitch and yaw.
     
    GroZZleR likes this.
  8. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Sorry, yeah. The deltas are just your Mouse X and Mouse Y values and you can clamp the rotation to whatever you want.
     
  9. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    It worked, but now I have another problem. I had already fixed it, but I don't know how I should do it with this code. I need to rotate the cube relative to the world space, and not the local space. How can I do it?
     
  10. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Use rotation instead of localRotation.
     
  11. Pedro_R

    Pedro_R

    Joined:
    Dec 7, 2017
    Posts:
    50
    Unfortunately it didn't work as I expected. While I'm looking at the front face of the cube, it works correctly:

    If I drag the mouse down, the front face goes down
    If I drag the mouse up, the front face goes up.

    However, when I look at any other face, it doesn't work as I want. If I look at the back face, for example, it works like that:

    If I drag the mouse down, the front face goes up
    If I drag the mouse up, the front face goes down.

    And that happens when I use transform.rotation() as well as when I use transform.localRotation().

    How can I fix this?
     
  12. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The easiest way to solve this new issue is to parent the cube to another object, call it Turntable or something, and then rotate the Turntable to face the camera. Then go back to using localRotation for the cube itself. It should work 100% like you want now.