- Home /
GetKeyDown works only once
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");
}
Answer by SergioSandiaz · Sep 19, 2016 at 06:00 AM
Well... that´s normal, I can´t understand your problem but
Input.GetKeyDown
is designed for work only once, the you need to release it and click again, you can use
Input.GetKey
that one returns true while the user press the key, Hope that helps you
Answer by Bunny83 · Sep 18, 2016 at 11:44 PM
Well, you have those two variables:
facingRight
facingLeft
which you check in your if statement and toggle inside the statement. So once you execute the if the state of the variable will flip and you can't enter the if again. That's literally what you code does. I'm not sure what those variables are good for. At the moment their only use is to prevent the if statement from being executed multiple times.
In general, why have 2 booleans to represent a state that can have 2 values. You are facing right or left. You probably shouldn't be able to face "right and left" or "not right and not left". If you need up and down too, make an enum or something
public enum Facing {
Up,
Down,
Left,
Right
}
Sure you can make it work with booleans but for example debugging gets a lot easier if your variables can't end up in invalid states
Your answer
Follow this Question
Related Questions
Complicated Rotation Issue 1 Answer
Unity Simulate Local Rotation 0 Answers
Code to rotate around a local axis until local Y is 0? 0 Answers
Child versus Parent rotations 3 Answers
is there any way to get a sane number on an objects current y rotation? 2 Answers