c# – Unity 3D Platformer Movement issue when trying to rotate the model to face the direction it moves
I am trying to create movement where the rigidbody player faces the direction they are moving. I want to add a slight turn and make them walk backwards slowly without turning ala Megaman Legends but I digress.
Using my movement code rotating while moving always causes this sort of pivotting instead of rotating where if I hold left or right It will pivot around a central point instead of simply change the model facing direction.
private void FixedUpdate()
{
Move();
}
private Vector3 previousMoveDirection;
private void Move()
{
Vector3 moveDirection = new Vector3(moveInput.x, 0f, moveInput.y) * moveSpeed;
if (moveDirection != Vector3.zero)
{
previousMoveDirection = moveDirection;
Quaternion targetRotation = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.Euler(0f, targetRotation.eulerAngles.y, 0f);
}
else
{
Quaternion targetRotation = Quaternion.LookRotation(previousMoveDirection);
transform.rotation = Quaternion.Euler(0f, targetRotation.eulerAngles.y, 0f);
}
rb.velocity = new Vector3(moveDirection.x, rb.velocity.y, moveDirection.z);
}
What is going wrong? How should I be doing this, movement is ok when im not rotating.
Read more here: Source link

