Help limiting Camera rotation, spinning out of control.
Got camera code that kind of works but it rotates weird when i spin the courser. i think the main problem is lack of limits on the cameras vertical rotation. Help me put limits on it stopping it from moving more that 180 degrees vertically?
void Update()
{
float horizontal = Input.GetAxis("Mouse X");
transform.Rotate(new Vector3(0, horizontal * 10f, 0));
float vertical = Input.GetAxis("Mouse Y");
transform.Rotate(new Vector3(vertical * -5f, 0, 0));
}
Have you looked into $$anonymous$$athf.Clamp to set a $$anonymous$$ and max value of the current rotation?
Answer by Namey5 · Nov 04, 2016 at 08:56 AM
In your case, where you have "transform.Rotate(new Vector3(vertical * -5f, 0, 0));" you would replace it with a different kind of statement, i.e.
private float rotX = 0;
...
void Update ()
{
float horizontal = Input.GetAxis ("Mouse X");
transform.Rotate (new Vector3 (0, horizontal * 10f, 0));
float vertical = Input.GetAxis ("Mouse Y");
rotX += vertical * -5;
rotX = Mathf.Clamp (rotX, -90, 90); //This clamps the vertical rotation in a 180 degree angle
transform.localRotation = Quaternion.Euler (new Vector3 (rotX, transform.localRotation.eulerAngles.y, transform.localRotation.eulerAngles.z));
}
This does essentially the same thing, but actually sets the rotation directly rather than using transform functions.
Your answer
Follow this Question
Related Questions
How do i limit the rotation of a game object in relation to a free following camera ? 0 Answers
No overload for method 'RotateAround' takes one argument 1 Answer
Making the player the camera's axis of rotation. 0 Answers
Problems with rotation after zoom (camera like Sketchfab) 0 Answers
Recognize when ever camera looks up and turns back down 0 Answers