Rotate object with mouse in parent position

Hi, I’m trying to rotate an object pointing to the mouse pointer. This object is child of the character object.

This code functions well until I move with the character object.
The problem is that it always follows screen or map’s zero position. So how can I make the child object to rotate always in parent z position?

You can watch the error here:
Error

This is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.RuleTile.TilingRuleOutput;

public class Pivot : MonoBehaviour
{
    public GameObject character;
    void FixedUpdate()
    {
        Vector3 diferencia = -Camera.main.ScreenToWorldPoint(Input.mousePosition - transform.position);
        diferencia.Normalize();
        float rotacionZ = Mathf.Atan2(diferencia.y, diferencia.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotacionZ);
    }
}

Thanks in advance.

The problem is that you’re mixing Input.mousePosition which is a position in screen space with the transform.position which is a position in world space.
You need to first bring the mouse to world space THEN do the calculation.
Should look like this

using UnityEngine;

public class Pivot : MonoBehaviour
{
    public GameObject character;
    void FixedUpdate()
    {
        Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3 dir = mouseWorldPos - transform.position;
        dir .Normalize();
        float rotacionZ = Mathf.Atan2(dir .y, dir .x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotacionZ);
    }
}

@bloodthirst69 I understand what you say, I changed my code and the behaviour changed a lot.
This is the code:

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

public class Pivot : MonoBehaviour
{
    public GameObject character;

    void FixedUpdate()
    {

        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        Vector3 dir = mouseWorldPosition - transform.position;

        Debug.Log($"Dir: {dir} - mouseWorldPosition: {mouseWorldPosition}");
        
        dir.Normalize();

        float rotacionZ = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

        transform.rotation = Quaternion.Euler(0f, 0f, rotacionZ);

    }
}

This is the behaviour:
Error1

In resume, now it doesn’t read my mouse movement, instead it moves when I move the character.

So if someone is looking for the solution:
First I have to put dir z = 0 before normalize. The code look like this:

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

public class Pivot : MonoBehaviour
{
    public GameObject character;
    public void Update()
    {
        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        mouseWorldPosition.z = 0f;
        Debug.Log($"mouseWorldPosition: {mouseWorldPosition}");

        Vector3 dir = mouseWorldPosition - transform.position;

        dir.z = 0f;
        dir.Normalize();

        Debug.Log($"dir: {dir}");


        float rotacionZ = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;

        transform.rotation = Quaternion.Euler(0f, 0f, rotacionZ);

    }
}

Then I had to change the projection to Orthographic instead of Perspective, since it’s a 2D game and it doesn’t have perspective.

image

For more information about this problem and the solution look at this page:
ScreenToWorldPoint