- Home /
Trying to clamp camera position causes it to get stuck for a few seconds.
yaw += camSpeed * Input.GetAxis("Mouse X");
pitch -= camSpeed * Input.GetAxis("Mouse Y");
Vector3 direction = new Vector3(Mathf.Clamp(pitch, -90, 90), yaw, 0.0f);
Camera.main.transform.rotation = Quaternion.Euler(direction);
My code for rotating the camera is the above. However, when I try using this code, it works... mostly. When I go to one of the limits for long enough, it gets stuck there unless I apply a lot of force moving it back. How would I fix this?
Answer by KoenigX3 · Jun 02, 2020 at 07:40 AM
Don't clamp the value in the new vector, clamp it when you are managing input.
pitch = Mathf.Clamp(pitch - camSpeed * Input.GetAxis("Mouse Y"), -90, 90);
When you clamped in new Vector3, you did not clamp pitch itself, you clamped the x component of your new vector. Pitch continued to rise above 90 and fall above -90. Now, since pitch is clamped, you can simply use:
Vector3 direction = new Vector3(pitch, yaw, 0);
Your answer
Follow this Question
Related Questions
FPS Camera Control rotation limits 1 Answer
How to smoothly rotate camera around an object with touch 1 Answer
How do I stop my camera flying? 0 Answers
Why is my camera not snapping 90 degrees? 0 Answers
How to force camera rotation? 1 Answer