Minus Degree Rotation

I’m working on a camera script and i want to make that when i’m moving to the left, camera turns to 90 degree left and when i’m moving right, camera turns 90 degree right(from the back of the character). I’m not copying the full script:

var horizontal = Input.GetAxis("Horizontal");

else if(horizontal == 1 || horizontal == -1){
			Debug.Log(Input.GetAxis("Horizontal"));
			wantedRotationAngle = 90;
			wantedRotationAngle *= horizontal;
			
			currentRotationAngle = Mathf.Lerp(currentRotationAngle, wantedRotationAngle, Time.deltaTime * rotationDamping);
			
			currentHeight = Mathf.Lerp(currentHeight, wantedHeight, Time.deltaTime * heightDamping);
			
			var currentHRotation = Quaternion.Euler(0, currentRotationAngle, 0);
			
			
			transform.position = target.position;
			transform.position -= currentHRotation * Vector3.forward * distance;
			
			transform.position.y = currentHeight;

And the problem is: When i’m moving to the left(horizontal = -1) it tries to turn 270 degrees. And no problem with right. How can i make it turn 90 degrees opposite direction?

I think the issue is that your Lerp rotation calculates from 0 to 270 (meaning 0, 1, 2, 3 etc until 270).

What you need to do, is do a calulation that “subtracts” instead of adding (meaning 0, 359, 358, 357 etc until 270). I don’t think you can do something like that with Lerp in a easy way.

Good luck!

Try lerping between two Quaternions instead of numbers. Floats will lerp between 0 and 270 only in positive direction, since they are not circular. Quaternions will use the shortest distance between two rotations.

Something like this:

transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(0, wantedRotationAngle, 0), Time.deltaTime * rotationDamping);