- Home /
Problem with rotation of character in 2d gameplay (LookRotation and Quaternions)
Hi!
I'm trying to sort out the rotation of my character. The gameplay is all 2d so he'll either face left or right and I declare the boolean faceRight depending on whether the left input was last pressed or the right input.
The code below works perfectly when the character rotates with his face towards the camera however when the character rotates with his face away from the camera he just looks away from the camera (instead of left or right).
I think if I can control which direction he rotates (force to rotate with his face looking at the camera) that will work, because when he faces away, the variable "allAxisRotation.y" stops at 1. I know this is a quaternion and I think that may be the problem.
Also, there are two empty GO's named 'degrees0' and 'degrees180' in the script. They are positioned very far away from the gameplay at both -1000 on the x and +1000 on the x with both of them being on the z=0 plane. These are the reference points for the "targetPoint" variable in the script.
Here is my code so far;
if (faceRight)
{
targetPoint = degrees0.transform.position;
targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
allAxisRotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmoothingSpeed);
transform.rotation.y = allAxisRotation.y;
}
else if (!faceRight)
{
targetPoint = degrees180.transform.position;
targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
allAxisRotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmoothingSpeed);
transform.rotation.y = allAxisRotation.y;
}
Any thoughts on how I can fix this issue?
Thanks.
Here's a link to the corresponding forum thread: http://forum.unity3d.com/threads/87648-Problem-with-rotation-of-character-in-2d-gameplay-(LookRotation-and-Quaternions).
Your answer
Follow this Question
Related Questions
Rotation from AngleAxis() ToAngleAxis() flips back and forth 1 Answer
Root Animations are inaccurate 0 Answers
How to always rotate clockwise 1 Answer
Smooth rotation in 90° increments 0 Answers