How to limit rotation from RotateAround?
I'm setting up a camera system to let the user pan the camera with an SHIFT + RMB + drag movement, as well as rotate the view around a point in space with RMB + drag.
So far, i've done this with an empty "CameraHolder" gameobject, with the mainCamera as child. The CameraHolder rotates about its y-axis to orbit left/right, and translates to pan the camera. The mainCamera uses transform.RotateAbout to orbit around the CameraHolder in the up/down direction.
However, i'm having trouble locking the up/down direction to 90 degrees up/down from the neutral plane. The camera either just continues in a circle around the CameraHolder, or rotates around the pole, both resulting in the controls for up/down inverting. I'd like to limit the movement to going from directly above, looking down upon the point to directly below, looking up to the point. So far, this is my code:
//Re-align camera towards cameraholder.
Camera.main.transform.LookAt(CameraHolder);
//RMB + drag
//orbit up/dn/l/r
if (Input.GetMouseButton(1) && !Input.GetKey(KeyCode.LeftShift)){
CameraHolder.Rotate(new Vector3(0,1,0) * Input.GetAxis("Mouse X")); //orbit the view l/r by rotating CameraHolder
Camera.main.transform.RotateAround(CameraHolder.position, Vector3.left, Input.GetAxis("Mouse Y")); //orbit the view up/dn by rotating camera around CameraHolder
}
//SHIFT + RMB + drag
//translate up/dn/l/r
if (Input.GetMouseButton(1) && Input.GetKey(KeyCode.LeftShift)){
CameraHolder.Translate( new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0)); //translate the view by translating CameraHolder
}
I've tried using Mathf.Clamp, but didn't manage to figure out how to apply that to a RotateAround. Any help with limiting the up/down orbit would be greatly appreciated.
Answer by neosca · May 24, 2017 at 12:05 PM
You can do something like this to clamp rotation in a particular axis:
float sensitivity = 4f;
Camera.main.transform.LookAt(CameraHolder);
if (Input.GetMouseButton(1) && !Input.GetKey(KeyCode.LeftShift)){
float newRotationX =Camera.main.transform.localEulerAngles.x - Input.GetAxis ("Mouse Y") * sensitivity;
newRotationX = Mathf.Clamp (newRotationX, 35f, 75f);
float newRotationY = Input.GetAxis ("Mouse X") * sensitivity;
Camera.main.transform.RotateAround(CameraHolder.position, Vector3.up, -newRotationY );
float rotateX = newRotationX - Camera.main.transform.localEulerAngles.x;
Camera.main.transform.RotateAround(CameraHolder.position, Camera.main.transform.left, rotateX );
}
Thanks for your answer. While this does work for most values, it sadly doesn't work for 90 and 0 degrees, or for going below the horizontal plane.
Your answer
Follow this Question
Related Questions
Rotating Camera around Player's X-axis while rotating Player around its Y-axis (using mouse input) 0 Answers
Rotate an object around one of his childs in the self space (not the world space) 2 Answers
AngleAxis and MoveRotation not rotating around the object 0 Answers
Axis are not rotated as I would expect. 0 Answers