- Home /
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?
Answer by Piflik · Jan 03, 2013 at 10:30 AM
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);
Well thanks, it worked, but; is there any way i can use Quaternion.Lerp without converting every variable i used, to quaternion? Because this solution comes with additional 3 lines for me to convert degrees like this:
var quatWantedRotationAngle = Quaternion.Euler(0, wantedRotationAngle, 0);
var quatCurrentRotationAngle = Quaternion.Euler(0, currentRotationAngle, 0);
var QLCurrentRotationAngle = transform.rotation;
and then:
QLCurrentRotationAngle = Quaternion.Lerp(quatCurrentRotationAngle, quatWantedRotationAngle, Time.deltaTime * rotationDamping);
Answer by CodeMasterMike · Jan 03, 2013 at 08:44 AM
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!
Your answer
Follow this Question
Related Questions
Camera not rotate when following player 1 Answer
Object rotates with camera 2 Answers
Change rotation by script 2 Answers
Transition current camera rotation to 0,0,0 1 Answer
Help, please with rotation input 0 Answers