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);
}
}