- Home /
Make Quaternion.Slerp Less Jittery
Hello, I have created a simple state machine for a 3rd person camera system. In my state machine I have different actions the player can do, WalkLeft, WalkRight, Forward, etc. In my WalkingLeft state I have tried to set up a nice rotation for the player. However the rotation is very jittery, within a 30 degree angle.
Here is my code for the walkingleft state.
void WalkingLeft()
{
//transform.Rotate(0, 270, 0);
//Debug.Log(transform.rotation);
//Debug.Log(transform.eulerAngles.y);
Quaternion target = Quaternion.Euler(transform.rotation.eulerAngles.x, (transform.rotation.eulerAngles.y + 270.0f), transform.rotation.eulerAngles.z);
transform.rotation = Quaternion.Slerp(transform.rotation, target, rotateSpeed * Time.deltaTime);
Debug.Log(Quaternion.Angle(transform.rotation, target));
anim.CrossFade("Walking");
}
"Left" must be relative to the player, I think my issue is somewhere in setting the target but I'm not sure what exactly to change.
The goal of the smooth rotation is to make it where the character doesn't instantly face left or right. The character should rotate to what is right and left.
Thanks for your time.
Answer by toromano · Oct 26, 2015 at 07:49 PM
If only problem is jittering, Slerp could be the reason. Because your transform.rotation and target rotation is constantly changing. You need to hold your rotation before turning to left:
private Quaternion rotationBeforeLeft;
void WalkingLeft()
{
//transform.Rotate(0, 270, 0);
//Debug.Log(transform.rotation);
//Debug.Log(transform.eulerAngles.y);
Quaternion target = target = Quaternion.Euler(rotationBeforeLeft.eulerAngles.x, (rotationBeforeLefty + 270.0f), rotationBeforeLeft.eulerAngles.z);
transform.rotation = Quaternion.Slerp(rotationBeforeLeft, target, rotateSpeed * Time.deltaTime);
Debug.Log(Quaternion.Angle(transform.rotation, target));
anim.CrossFade("Walking");
}
Thank you, that seems to have stopped the jittering. Oddly enough, it doesn't seem as though it is actually 'Slerping.' The model appears to snap from one direction to another. Even though the rotateSpeed is .1f. Do you know why that may be?
You might try changing target Quaternion target = rotationBeforeLeft * Quaternion.Euler(0, -90, 0);
I'll give that a shot next, I noticed its only snapping on left. The code for all the other directions is the same, $$anonymous$$us the angle, it's very odd.
EDIT: There must of been some small typo, because I just copied the code for right movement and it worked.
Thanks for your help. I appreciate it.
Your answer
Follow this Question
Related Questions
Basic AI Locked Axis 1 Answer
Rigidbody2d Collision Breaks Quaternion.Slerp 2 Answers
Best rotation function to use 1 Answer
Use Lerp to rotate object 2 Answers
Follow and Rotate an object 1 Answer