I have a simple top-down game, with the use of the new Input system I allow the player to move but when I try to make him face the direction his is moving, for some reason the player never looks at the right direction no matter what values I had put in.
public float moveSpeed;
private Vector2 move;
public void OnMove(InputAction.CallbackContext context) {
move = context.ReadValue<Vector2>();
}
private void Update() {
// This is the movement which works fine
Vector3 movement = new Vector3(move.x, move.y, 0f);
transform.Translate(movement * moveSpeed * Time.deltaTime, Space.World);
// This doesnt want to work
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement, Vector3.up),0.01f);
}
LookRotation needs a “forward” and a “up” arguments. The default is intended for 3D but I do it in 2D. When I press “D” the movement is equal (1, 0, 0) which is the +X direction, if I put +Z for the up direction, shouldnt it rotate on the Z axis. No.
No matter what I tried, It rotated on the other X and Y axis instead of the Z.
SOLUTION
This is what I did and It works fine:
if(movement.magnitude > 0)
transform.rotation = Quaternion.LookRotation(Vector3.forward, movement) * Quaternion.Euler(0f, 0f, 90f);
And to smooth it I added Quaternion.Slerp(), like this:
Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward, movement) * Quaternion.Euler(0f, 0f, 90f), 0.1f);