- Home /
Movement behaves differently on X axis
Hello,
I have a sprite that is supposed to look to the mouse and follow it, like a top-down game.
I have this script that adds 2 different velocities to the player. One for Y axis, and one for X axis. The problem is that the player has correct movement on the Y axis (when I look up it goes up with a speed of 1, when I look down it goes down with a speed of -1.) but on the X axis behaves weird...
function Update () {
var mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
var rot = Quaternion.LookRotation(transform.position - mousePos, Vector3.forward );
transform.rotation = rot;
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z + 90);
Debug.Log(transform.eulerAngles.z);
// I use a function that calculates the correct speed for each axis.
// For example when the player looks up-right (transform.eulerAngles.z = 45) y velocity should be 0.5 and x velocity should be 0.5.
rigidbody2D.velocity.y = (90 - Mathf.Abs(transform.eulerAngles.z - 90))/90;
rigidbody2D.velocity.x = (90 + Mathf.Abs(transform.eulerAngles.z - 90))/90 - 1;
}
Any help?
Comment