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

Limit Rotation of an object

Discussion in 'Scripting' started by KonniosGames, Oct 27, 2021.

  1. KonniosGames

    KonniosGames

    Joined:
    Dec 22, 2018
    Posts:
    18
    So basically I made an object rotate when I click and drag it but I want to limit the rotation to (50, -50) on the X axis, and (30, -30) on the Y axis. I tried a lot of things but nothing seems to work so far...
    Here's my code:
    private void OnMouseDrag()
    {
    float XaxisRotation = Input.GetAxis("Mouse X") * rotationSpeed;
    float YaxisRotation = Input.GetAxis("Mouse Y") * rotationSpeed;
    transform.Rotate(Vector3.down, -XaxisRotation, Space.Self);
    transform.Rotate(Vector3.right, -YaxisRotation, Space.Self);
    }

    (I will appreciate it if you help me.)
    (If you can't, is there a better way to rotate the object?)
     
    Tijl_engelhart and TheDevloper like this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Track the float angle yourself, adjust it, clamp it, then use it to drive the rotation.

    Code (csharp):
    1. transform.rotation = Quaternion.Euler( UpDownAngle, LeftRightangle, 0);
    It might also suit you better to split the vertical and horizontal rotations into two separate parented transforms.
     
    RyanJVR and StarManta like this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Yep, there is. Track your own X and Y rotations - that is, add your GetAxis results to a class-level variable. Then use transform.rotation = Quaternion.Euler(y,x,0) to set it. Once you've got that, you can easily Mathf.Clamp your tracked rotation values to anything you like.

    Lots more information about rotations in Unity:
     
    RyanJVR and Kurt-Dekker like this.
  4. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    you can use the mathf.clamp method, like this

    private void OnMouseDrag()
    {
    float XaxisRotation = Input.GetAxis("Mouse X") * rotationSpeed;
    float YaxisRotation = Input.GetAxis("Mouse Y") * rotationSpeed;
    XaxisRotation = Mathf.Clamp (, -maxX, maxX);

    XaxisRotation = Mathf.Clamp (XaxisRotation.x, -50, 50);
    YaxisRotation= Mathf.Clamp (XaxisRotation.x, -50, 50);

    transform.Rotate(Vector3.down, -XaxisRotation, Space.Self);
    transform.Rotate(Vector3.right, -YaxisRotation, Space.Self);
    }

    please test and let me know.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Without keeping track of the rotation per frame (as both replies before you said to do), this will only clamp the amount you can rotate every frame.

    And will also give the OP at least.... I'm counting 3 syntax errors?

    And you didn't use code tags.
     
    RyanJVR and Kurt-Dekker like this.
  6. KonniosGames

    KonniosGames

    Joined:
    Dec 22, 2018
    Posts:
    18
    I FINALLY DID IT WITHOUT CHANGING TOO MUCH ON MY CODE!!! I found a tutorial by Brackeys on how to do fps movement. Here's the final code if anyone's interested...

    private void OnMouseDrag()
    {
    float XaxisRotation = Input.GetAxis("Mouse X") * rotationSpeed;
    float YaxisRotation = Input.GetAxis("Mouse Y") * rotationSpeed;
    XRotation -= YaxisRotation;
    XRotation = Mathf.Clamp(XRotation, -40f, 40f);
    YRotation -= XaxisRotation;
    YRotation = Mathf.Clamp(YRotation, -40f, 40f);
    transform.parent.localRotation = Quaternion.Euler(XRotation, -YRotation, 0f);


    }
     
  7. Tiberius1701

    Tiberius1701

    Joined:
    Sep 16, 2014
    Posts:
    68
    Thank you for this @KonniosGames - I was scouring the net looking for an answer to this very problem, and your code worked like a charm!