I’m trying to write the script for my camera, where when the player pushes the right analog stick in a direction, it rotates the camera. However I’m stuck trying to figure out how to prevent the camera from rotating past 90 degrees on the x axis because when it WOULD get to 91 degrees, it instead rotates the y and z axes 180 degrees and makes the x rotation 89. I understand this is because of Quaternion stuff, but my problem is that this prevents me from saying “if x rotation <= 90, move the camera” because it’s ALWAYS <= 90. And I can’t assume anything about the other 2 axes of rotation because the player can rotate horizontally and vertically. So with this auto-rotation weirdness, how can I prevent the player from rotating the camera past 90degrees?
EDIT: TL;DR picture http://i.imgur.com/0Knh34Y.png
Sorry if this doesn’t help (I’m a noob) , would it be possible to set the rotations seperately? e.g
transform.rotation.x = 90;
transform.rotation.y = 0;
transform.rotation.z = 0;
if you want to limit the rotations what you can do is track the forward vector instead of using the angles directly
so say you define a
Vector3 forward;
void Awake()
{
forward = new Vector3(0,0,1);
}
and then in your code basically add to the X and Y values the current analog stick axes,
void Update()
{
forward.x += Input.GetAxis("Horizontal") * 0.1f;
forward.y += Input.GetAxis("Vertical") * 0.1f;
then your vector rotates between -1,-1,1 and 1,1,1, the Z never flips
transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
}
EDIT:
If you also normalize the vector after adding the axes you get a more constant rotation speed. The rotation slows down at the end points either way because the X and Y values become less and less significant. You could also actually rotate the vector using
float x = Input.GetAxis("Horizontal") * 0.1f;
float y = Input.GetAxis("Vertical") * 0.1f;
forward = Vector3.RotateTowards(forward, Vector3.right, x, 0f);
forward = Vector3.RotateTowards(forward, Vector3.up, y, 0f);
forward.z = Mathf.Max(forward.z, 0.01f);
forward = forward.normalized;
Here you need to always keep the Z a positive number manually because at the end it reaches 0 and then when rotating back to a different angle the function doesn’t know which direction to go.