Help with Quaternion.Slerp movement?
Hey, I've been having some trouble with a quaterion.slerp code. I want the movement of my object to rotate based on the wasd key press. It works, for the most part. However, my object keeps rotating 90 degrees on the z-axis on play? I thought it was the model having the wrong axis forward, since it was imported from Blender, but that doesn't seem to have solved my problem. The code I used is below. I'm still a beginner at this stuff, so I'm wondering if it's something I did wrong?
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, 0f, Input.GetAxis("Vertical") * moveSpeed);
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection), turnSpeed);
controller.Move(moveDirection * Time.deltaTime);
Answer by inkydraco · Apr 03, 2019 at 04:13 AM
Nevermind, I figured it out! So I read that the Quaterion.LookRotation
code works in the sense of Quaternion.LookRotation(a vector3 that will face forward, a vector3 that will face up)
.
The moveDirection
variable was being affected with the gravity code I had written moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
so it kept making the y axis point forward and flipping my character down. As soon as I commented that piece of code out, the model moves wonderfully and faces in the correct direction!
Now I just need to figure out how to readd the gravity again, without affecting the quaternion. Hope this helps someone out, if you're having a similar issue.