Rotate sprite to face direction of movement

Hey folks, I have been bashing my head on this for a week now trying out all kinds of different methods and I feel like this should be easier than I’m making it. However nothing I have tried thus far works.

I am making a top down sprite character that moves exactly with the mouse cursor. This part works just fine, but I also want it to face the direction it is moving in but I just cannot get it to rotate on the Z axis so that it remains flat to the camera but rotates around to face its movement direction.

I’ve been trawling through various topics here, on youtube, stackoverflow, I am at a loss and its driving me nuts that I feel it should be a really simple thing to achieve but it just doesn’t seem to be a done thing. a lot of the “follow cursor” methods require the object to follow the direction of the cursor, not be exactly on the cursors position, which, isn’t what I’m aiming at. I want it to be exactly at the point of the cursor.

Here’s my code, what rotation to direction of travel method using this cursor movement method would you recommend here?

{
    private Vector3 pos;


    void Awake()
    {
        //Cursor.visible = false;

    }


    void Update()
    {
        pos = Input.mousePosition;
        pos.z = 980f; //positions at Z =0 from the camera, camera is at -980 Z.

        Debug.Log("pos.x");
        Debug.Log("pos.y");

    }

    private void FixedUpdate()
    {

        transform.position = Camera.main.ScreenToWorldPoint(pos);
    }
}
  • Start with finding mouse position in world space coordinates:

    Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    will find the position of the mouse in space, so with that information, you can write

    Vector3 direction = (transform.position - worldPosition);
    (an algorithm that finds the direction of an object in comparison to another)

  • Then, turn that into a Quaternion using

    Quaternion wantedRotation = Quaternion.Euler(direction);
    and finally actually set the rotation using some fancy spherical interpolation (s-lerp)

    transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, turnSpeed)
    I think that the slowing at the end looks better than not btw.
    _
    TL;DR:

    Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector3 direction = (transform.position - worldPosition);
    Vector3 wantedRotation = Quaternion.Euler(direction);
    transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, turnSpeed);
    _