Mouse Location in world relative to the player

So I have a 2D pixel art character in a 3D enviroment using x and z to move about. I’m using blend trees and x and z locations to play animations based on direction, so I am currently track the mouse position relative to the player. It works on the x but not on the z (up and down the screen), it seems to slowly go out of sync I think its because of the camera being at a angle but I’m unsure. I’m using the x and z location of the mouse in the world relative to the player to play the correct facing animations upright, upleft, downright, downleft. Like I said x works perfectly and stays relative where as y or 3d space z doesn’t.

I hope this makes sense and someone can help? Many thanks in advance.

Below is my script:

public class PlayerRotation : MonoBehaviour {

	//Mouse Input variable (for offsetting)
	public Vector3 mouseInput;

	//Get animator
	public Animator anim;

	// Use this for initialization
	void Start () {

	}	
	
	// Update is called once per frame
	void Update () {


		//Get Mouse Position
		Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        //Get Player Position
        Vector3 PlayerPosition = transform.position;
        //Offset mouse offset -0.5f for offseting object in game
        mouseInput = new Vector3(mousePos.x - PlayerPosition.x, mousePos.y - PlayerPosition.z, mousePos.z - PlayerPosition.z);

        //If mouse is on left flip image
        if (mouseInput.x < 0) {
        	//flip image left
        	transform.GetChild(0).localRotation = Quaternion.Euler(0, 180, 0);
        } else {
        	//else flip the image right
        	transform.GetChild(0).localRotation = Quaternion.Euler(0, 0, 0);
        }

        //Set animation parameters for animations
        anim.SetFloat("xInput", mouseInput.x);
        anim.SetFloat("zInput", mouseInput.y);

        //Debugging purposes
        Debug.Log( PlayerPosition);
        Debug.Log( mouseInput);

 
	}
}

Is it on purpose that on mouseInput you do mousePos.y - PlayerPosition.Z? Instead of mousePos.y - PlayerPosition.Y

I gave an answer to this on your last question. You should raycast from your mouse to a plane in the world to account for the angle of the camera. Inserting it into your code should be this:

public class PlayerRotation : MonoBehaviour {
 
     //Mouse Input variable (for offsetting)
     public Vector3 mouseInput;
 
     //Get animator
     public Animator anim;
 
     // Use this for initialization
     void Start () {
 
     }    
     
     // Update is called once per frame
     void Update () {

        // make a plane in the world level to the player
        Plane plane = new Plane(transform.up, transfrom.position);
        
        // create a ray from your mouse
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
        Vector3 mousePos = Vector3.zero;
        // cast the ray from mouse to the plane
        if(plane.Raycast(ray, out float distance)) {
             // get mouse position from where it hit in the world
            mousePos = ray.GetPoint(distance);
        }


         //Offset mouse offset -0.5f for offseting object in game
         mouseInput = mousePos - transform.position;
 
         //If mouse is on left flip image
         if (mouseInput.x < 0) {
             //flip image left
             transform.GetChild(0).localRotation = Quaternion.Euler(0, 180, 0);
         } else {
             //else flip the image right
             transform.GetChild(0).localRotation = Quaternion.Euler(0, 0, 0);
         }
 
         //Set animation parameters for animations
         anim.SetFloat("xInput", mouseInput.x);
         anim.SetFloat("zInput", mouseInput.z);
 
         //Debugging purposes
         Debug.Log( transform.position);
         Debug.Log( mouseInput);
 
  
     }
 }