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. Dismiss Notice

Clamping rotation on y-axis doesn't work [help]

Discussion in 'Scripting' started by sasa_dev, Apr 11, 2020.

  1. sasa_dev

    sasa_dev

    Joined:
    Apr 4, 2020
    Posts:
    10
    upload_2020-4-11_1-53-39.png

    (Update: please ignore this and read my comment underneath, I got it working but have a clamping problem)

    Hello guys I made my own camera script but I can't figure out one thing..
    I developed an over the shoulder style cam. Whenever the player moves his mouse horizontally, the player rotates around it's y axis (with the camera facing towards it's direction because it's a child in the player object). If he moves his mouse vertically, the camera will rotate around the player object (which works perfectly). However, what I wish to achieve is, when the player moves his mouse upwards, I want the camera to rotate up the x axis and y axis, so it rotates diagonally up to the players head and the camera kinda faces down from over the players shoulder.

    From:
    upload_2020-4-11_1-53-39.png

    To here:
    upload_2020-4-11_2-4-56.png

    hope you still understand what I mean. At first I changed the y-axis parameter to the rotationXAxis (which I use to rotate the cam on it's x-axis) in the "toRotation"-Quaternion. Worked perfectly when I just moved my mouse upwards. However, when I move the mouse horizontally, the player will still rotate around it's y axis but the camera won't face towards the players direction no more. I tried my best thinking mathematically but I couldn't find a solution. Sorry for the weird description of this problem, I'm not a native speaker and it's hard for me to describe this hahah.

    Thanks alot in advancé!

    Here's my script:
    Code (CSharp):
    1. void Update() {
    2.         mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    3.         mouseY = -(Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime);
    4.  
    5.         rotationXAxis += mouseY;
    6.         rotationXAxis = ClampAngle(rotationXAxis, -30f, 30f);
    7.  
    8.         player.Rotate(Vector3.up * mouseX);
    9.  
    10.         Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
    11.         Quaternion toRotation = Quaternion.Euler(rotationXAxis, transform.rotation.eulerAngles.y, rotationXAxis/8);
    12.         Quaternion rotation = toRotation;
    13.  
    14.         Vector3 negDistance = new Vector3(xPosOffset, yPosOffset, -distance);
    15.         Vector3 position = rotation * negDistance + player.position;
    16.      
    17.         transform.rotation = rotation;
    18.         transform.position = position;
    19.  
    20.         mouseY = Mathf.Lerp(mouseY, 0, Time.deltaTime);
    21.     }
     

    Attached Files:

    Last edited: Apr 11, 2020
    PraetorBlue likes this.
  2. sasa_dev

    sasa_dev

    Joined:
    Apr 4, 2020
    Posts:
    10
    I have improved my script and now it works as wished. However I have a clamping problem, see in the video below how it overrotates at the end and fixes the camera onto a completely different y-direction around the player. Since it's based on the y-axis-movement of the mouse. However clamping the x axis worked, but not the y axis.



    Here's my current script:
    Code (CSharp):
    1. void Update() {
    2.         mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    3.         mouseY = -(Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime);
    4.  
    5.         rotationXAxis += mouseY;
    6.         rotationXAxis = ClampAngle(rotationXAxis, -30f, 30f);
    7.  
    8.         float rotationYAxis = mouseY;
    9.         rotationYAxis = ClampAngle(rotationYAxis, 0f, 30f);
    10.  
    11.         Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, 0, 0);
    12.         Quaternion toRotation = Quaternion.Euler(rotationXAxis, transform.rotation.eulerAngles.y - rotationYAxis, 0);
    13.         Quaternion rotation = toRotation;
    14.  
    15.         Vector3 negDistance = new Vector3(xPosOffset, yPosOffset, -distance);
    16.         Vector3 position = rotation * negDistance + player.position;
    17.      
    18.         transform.rotation = rotation;
    19.         transform.position = position;
    20.  
    21.         player.Rotate(Vector3.up * mouseX);
    22.         mouseY = Mathf.Lerp(mouseY, 0, Time.deltaTime);
    23.     }
    24.  
    25.     float ClampAngle(float angle, float min, float max) {
    26.         if (angle < -360F)
    27.             angle += 360F;
    28.         if (angle > 360F)
    29.             angle -= 360F;
    30.         return Mathf.Clamp(angle, min, max);
    31.     }
     
    PraetorBlue likes this.
  3. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    Oh yeah, sounds like a very familiar issue. It's related to Quaternions vs EulerAngles. Occasionally run into these kind of problems and never seem to get a grasp on how I fixed it.
     
    sasa_dev likes this.
  4. sasa_dev

    sasa_dev

    Joined:
    Apr 4, 2020
    Posts:
    10
    yeah, I've been working on the camera since yesterday. It's the first time I'm scripting a camera by my own and even though I still have to learn more physics to calculate rotations correctly in code it's a fun but also frustrating challenge.