Player pointing to mouse as if he's always at coordinates 0 0 0

I’m trying to make my player (in my 2D, top down game) point towards the mouse wherever he is ,but he points to the mouse as if he always is in the center of the world (0 0 0).
It works fine when he actually is in the center of the world but when he moves he just points to the mouse as if he still was there.
Here’s the relevant code:

`

  1. mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
  2. mousePos3D = new Vector3(mousePos.x, mousePos.y, 0f);
  3. Quaternion targetRotation = Quaternion.LookRotation(transform.forward, mousePos3D);
  4. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

`

mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
mousePos3D = new Vector3(mousePos.x, mousePos.y, 0f);
Quaternion targetRotation = Quaternion.LookRotation(transform.forward, mousePos3D);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

first I see that in mousePos3D, the mouseY needs to be in the Z coordinate, as Y is height.

Vector3 direction = moveTo - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);

Then your look vector needs to be more like this ^.

But I would debug.log your starting positions from the mouse click, and make sure those are right(or how you want them) first. Once you get screen to world point working, it should be smooth sailing from there.