- Home /
Camera clamp angle has issue at inspector angle of 0 degrees
I have looked at many similar questions and can not seem to find a relevant solution or better understanding of what is happening in unity.
Here is the code I am using, it is attached to a camera, which is the only child of my player gameobject. This function is called each frame in Update.
private void HandlePitchInput()
{
// Based on mouse Y input, rotate the camera about the local x axis towards the upper or lower pitchRange
float mouseYInput = Input.GetAxisRaw("Mouse Y"); // Returns a value between -1 and 1
if (mouseYInput != 0) // Are we recieving input, is the Player moving the mouse up/down?
{
float curPitch = transform.eulerAngles.x; // store our current rotation
curPitch -= mouseYInput * pitchSpeed * Time.deltaTime; // adjust pitch value
//Clamp range between inspector value of -30 and 70
if (curPitch < -30)
{
curPitch = -30;
}
else if (curPitch > 70)
{
curPitch = 70;
}
Quaternion newRot = new Quaternion();
newRot = Quaternion.Euler(curPitch, transform.eulerAngles.y, transform.eulerAngles.z); // create a new quaternion with the desired pitch without affecting pan or twist
transform.rotation = newRot; //set our x rotation equal to the new Quaternion
}
}
The variable pitchSpeed is currently 30f. Currently the greater than 70 clamp seems to be working just fine if I try to look down and the x angle in the inspector gets to 70 it stops there. However, whenever I reach 0 degrees (looking straight ahead) the camera snaps down to 70.
From looking around I think this has to do with the actual values being used by the quaternion under the hood being different from the Euler angles shown in the inspector. If the zero degrees reading in the inspector is not really zero degrees, then what is it? Why does the camera rotation snap only at zero?
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Clock script with custom time not working 0 Answers
How to lerp rotate the gun to the opposite of ur mouses X position and clamp it 1 Answer
How to rotate objects around point while always crossing a point? 2 Answers
Distribute terrain in zones 3 Answers