Hello, i’m new to this unity. so i have this code, when i press enter once, it always return true first then quickly return false. is this common thing? or is there something wrong in my code?
public float maxSpeed;
float accel = 0;
int check;
// Use this for initialization
void Start () {
accel = maxSpeed / 5;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Return) && check == 0) {
check = 1;
} else {
check = 0;
}
if (check == 1) {
transform.Rotate (Vector3.forward, maxSpeed * Time.deltaTime);
}
Debug.Log ("check = " + check);
}
According to the GetKeyDown documentation, the function returns true only on the frame when the key was pressed. It won’t return true again until the key is released and then pressed again, so that is the correct behavior. This would be used when you only want something to happen once on a keypress and not repeat over and over each frame.
If you want to check the actual state each frame then you’d use Input.GetKey instead which always gives you the current up/down state of the key.