2D rotation problem
I am building a top down game where the player can move in either the up, down, left, or right directions(arrow keys), but I am having trouble with my rotation. Here is my code.
public float timeBetweenMoves = 0.3333f; // Allow 3 moves per second
private float timestamp;
void Update () {
ArrowKeyMovement();
}
void ArrowKeyMovement()
{
if (Time.time >= timestamp)
{
if (Input.GetKey("up"))
{
transform.position = new Vector3(transform.position.x, transform.position.y + 1);
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 0, transform.rotation.w);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("down"))
{
transform.position = new Vector3(transform.position.x, transform.position.y - 1);
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 180, transform.rotation.w);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("left"))
{
transform.position = new Vector3(transform.position.x - 1, transform.position.y);
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 90, transform.rotation.w);
timestamp = Time.time + timeBetweenMoves;
}
else if (Input.GetKey("right"))
{
transform.position = new Vector3(transform.position.x + 1, transform.position.y);
transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 270, transform.rotation.w);
timestamp = Time.time + timeBetweenMoves;
}
}
}
The problem is that even though in the code for left or right says to adjust rotation of z to 90 or 270 whenever I run it in Unity it adjusts the rotation to 180. So pressing up moves the player up and rotates the sprite properly in the upward(0) direction. Down, left, and right all move the player in the correct directions however all 3(down, left, and right) rotate the sprite into the downward(180) orientation.
Thanks for any help!
Comment