how do I rotate the player to face the user input smoothly?

I was wondering how I could make my player rotate towards the user input smoothly, I’ve been using unity for some years but I still don’t get the rotation and quaternion things so I thought you may be able to help.

The ship moves automatically to where it’s looking at a constant speed but I can’t seem to figure out how to make it so that when you press “A” for example the ship automatically rotates towards 270 degrees and so on, which I feel would be much more intuitive than what I have now that simply rotates one way or another depending on the key you are pressing. It would be nice to also have some way of implementing it with “Input.GetAxis(“Horizontal”)” or something like that since I would like to adapt it to a ps4 controller instead of limiting it to just a keyboard

Thanks in advance :smiley:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShipController : MonoBehaviour
    public float speed = 5;
    public float rotationSpeed = 20;
    Rigidbody2D rb;
    BoxCollider2D Bcoll;
    // Start is called before the first frame update
    void Start()
        rb = gameObject.GetComponent<Rigidbody2D>();
        Bcoll = gameObject.GetComponent<BoxCollider2D>();

    // Update is called once per frame
    void Update()
    private void FixedUpdate()
        gameObject.transform.Translate(-Vector3.up * Time.deltaTime * speed);

        if (Input.GetKey(KeyCode.A))
            transform.Rotate(0, 0, Time.deltaTime * rotationSpeed);           
        }else if (Input.GetKey(KeyCode.D))
            transform.Rotate(0, 0, -Time.deltaTime * rotationSpeed);

So this will be a lot easier when using the horizontal and vertical axis instead of WASD, because you already have the axis going from -1 to 1. Its hard to explain how it works, so ill just write some code and comment a long the way:

//Set a speed for the rotation
float rotationSpeed = 5f;
//Get the position of the player.
Vector3 currentPosition = transform.position;
//Create a vector for the inputs (Which components you use depends on the game)
Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
//Adding these vectors together will result in a position in the world, that is around your player.
inputVector += currentPosition;

* The players position is at 0,0,0.
* Input is 1,0,0.
* The position in space would be 0,0,0 + 1,0,0 = 1,0,0.
* This would be to the right of the player.

//Now we create a target rotation, by creating a direction vector: (This would be just be inputVector in this case).
Quaternion targetRotation = Quaternion.LookRotation(inputVector - currentPosition);

//Rotate smoothly to this target:
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed);

Im not at home at the moment, so i can’t really test it properly, so it might not work as is. But im pretty sure this will get you in the right direction.