Hi, I want to make my character rotate in the direction which he is facing(the game is top view), and I encountered this problem:
Whenever I start the game, I start facing up, and I can press the arrow keys once. When I try to click again, my character moves, but does not rotate.
Right now I just care about rotating character in horizontal axis.
void Start () {
rb2d = gameObject.GetComponent<Rigidbody2D> ();
facingUp = true;
facingRight = true;
facingLeft = true;
facingDown = true;
}
// Update is called once per frame
void Update () {
float horizontal = Input.GetAxis ("Horizontal");
float vertical = Input.GetAxis ("Vertical");
HandleMovement (horizontal, vertical);
Flip (horizontal, vertical);
}
private void HandleMovement(float horizontal, float vertical) {
rb2d.velocity = new Vector2 (horizontal * speed, vertical * speed);
}
// this method resets the coordinates of the player
void Reset() {
if (!target)
target = GameObject.FindWithTag("Player");
}
if (horizontal < 0 && facingRight || horizontal > 0 && !facingRight && Input.GetKeyDown("right")) {
Reset ();
facingRight = !facingRight;
Vector3 rotationRight = transform.localEulerAngles;
rotationRight.z = 90;
transform.localRotation = Quaternion.Euler (0, 0, rotationRight.z);
Debug.Log (Input.anyKeyDown);
}
// left button
else if (horizontal < 0 && facingLeft || horizontal > 0 && !facingLeft && Input.GetKeyDown("left")) {
Reset ();
facingLeft = !facingLeft;
Vector3 rotationLeft = transform.localEulerAngles;
rotationLeft.z = -90;
transform.localRotation = Quaternion.Euler (0, 0, rotationLeft.z);
Debug.Log ("you pressed left");
}