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

(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:

To here:

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.

Here’s my script:

``````void Update() {
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = -(Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime);

rotationXAxis += mouseY;
rotationXAxis = ClampAngle(rotationXAxis, -30f, 30f);

player.Rotate(Vector3.up * mouseX);

Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, transform.rotation.eulerAngles.y, rotationXAxis/8);
Quaternion rotation = toRotation;

Vector3 negDistance = new Vector3(xPosOffset, yPosOffset, -distance);
Vector3 position = rotation * negDistance + player.position;

transform.rotation = rotation;
transform.position = position;

mouseY = Mathf.Lerp(mouseY, 0, Time.deltaTime);
}
``````

1 Like

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:

``````void Update() {
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = -(Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime);

rotationXAxis += mouseY;
rotationXAxis = ClampAngle(rotationXAxis, -30f, 30f);

float rotationYAxis = mouseY;
rotationYAxis = ClampAngle(rotationYAxis, 0f, 30f);

Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, 0, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, transform.rotation.eulerAngles.y - rotationYAxis, 0);
Quaternion rotation = toRotation;

Vector3 negDistance = new Vector3(xPosOffset, yPosOffset, -distance);
Vector3 position = rotation * negDistance + player.position;

transform.rotation = rotation;
transform.position = position;

player.Rotate(Vector3.up * mouseX);
mouseY = Mathf.Lerp(mouseY, 0, Time.deltaTime);
}

float ClampAngle(float angle, float min, float max) {
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
``````
1 Like

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.

1 Like

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.

1 Like