Third Person Camera - Issues to clamp rotation
Hi,
I'm currently working on a third person camera and have issues to clamp the Y axis value correctly. My first implementation works to some extent but had some limits since I want to change the "lookAt" transform in multiple scripts.
public Transform lookAt,
playerCamera;
public float mouseSpeed = 1.5f;
public float yAxisMin = -60f;
public float yAxisMax = 60f;
public bool invertedYAxis = true;
private float mouseX,
mouseY;
private void LateUpdate ()
{
if ( Input.GetAxisRaw( "Mouse ScrollWheel" ) != 0 )
CameraZoom();
if ( Input.GetMouseButton( 0 ) )
CameraMove();
}
private void CameraMove ()
{
mouseX += Input.GetAxis( "Mouse X" ) * mouseSpeed;
mouseY += ( invertedYAxis ) ? Input.GetAxis( "Mouse Y" ) * -mouseSpeed :
Input.GetAxis( "Mouse Y" ) * mouseSpeed;
mouseY = Mathf.Clamp( mouseY, yAxisMin, yAxisMax );
playerCamera.LookAt( lookAt );
lookAt.localRotation = Quaternion.Euler( mouseY, mouseX, 0 );
}
So what i was trying to do instead of adding the values to "mouseX+Y" and use them i tried to add the new values directly to the current rotation.
private void CameraMove ()
{
mouseX = Input.GetAxis( "Mouse X" ) * mouseSpeed;
mouseY = ( invertedYAxis ) ? Input.GetAxis( "Mouse Y" ) * -mouseSpeed :
Input.GetAxis( "Mouse Y" ) * mouseSpeed;
float clampedY = Mathf.Clamp( lookAt.localEulerAngles.x + mouseY, yAxisMin, yAxisMax );
playerCamera.LookAt( lookAt );
lookAt.localRotation = Quaternion.Euler( clampedY, lookAt.localEulerAngles.y + mouseX, 0 );
}
To my suprise this method actually worked, but just as long as I didn't try to clamp the values. When i did the moment I go under 0 degrees i get clamped to the "yAxisMax" value. I understand from searching in the forum that the issue is caused by using the "localEulerAngles" variable, but I havn't really found another way to access the current rotation and increment the change.
Should i just try to clamp with values that suits 360 degrees or is there a "nicer" way to handle this?
Thanks in advance!