Input.GetKey acting weird

Hello,I encountered a strange problem with Input.GetKey…
I put Input.GetKey into update. If the key is pressed, it sets a bool to true, if not it sets the bool too false. I tested it by printing the result of the if statement to the console, but it acts really weird.
Once the button is pressed it goes like this:
True
False
False
True
False
True

It seems to be completly random. Ive no clue. As long as the button is pressed leftward should be true, but its like i let go of the button for a millisecond…

Would be nice if someone could help me,
thanks in advance, Fabian

using UnityEngine;
using System.Collections;

public class Control1f1 : MonoBehaviour {
    public Rigidbody rb;

    int times = 0;
    int right = 0;
    int left = 0;

    bool forward = false;
    bool backward = false;
    bool rightward = false;
    bool leftward = false;
    bool upward = false;
    bool downward = false;
    // Use this for initialization
    void Start () {

    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetKey (KeyCode.W)) {
            forward = true;
        } else {
            forward = false;
        }

        if (Input.GetKey (KeyCode.S)) {
            backward = true;
        } else {
            backward = false;
        }

        if (Input.GetKey (KeyCode.D)) {
            rightward = true;
        } else {
            rightward = false;
        }

        if (Input.GetKey (KeyCode.A)) {
            leftward = true;
        } else {
            leftward = false;
        }

        if (Input.GetKey (KeyCode.E)) {
            upward = true;
        } else {
            upward = false;
        }

        if (Input.GetKey (KeyCode.Q)) {
            downward = true;
        } else {
            downward = false;
        }
        Debug.Log (leftward);
    }

    void FixedUpdate () {
        Debug.Log (rb.velocity.magnitude);

        Forward ();
        Backward ();
        Rightward ();
        Leftward ();
        //Upward ();
        //Downward ();
    }

    void Forward ()
    {
        if (forward && times < 400) {
            rb.AddForce (transform.forward * 50, ForceMode.Acceleration);
            times++;
        }
    }

    void Backward ()
    {
        if (backward && times > -400) {
            rb.AddForce (transform.forward * -1 * 50, ForceMode.Acceleration);
            times--;
        }
    }

    void Rightward ()
    {
        if (rightward) {
            if (right < 100) {
                rb.AddForce (transform.right * 5, ForceMode.Acceleration);
                right++;
            }
        } else {
            if (right > 0) {
                rb.AddForce (transform.right * -1 * 5, ForceMode.Acceleration);
                right--;
                Debug.Log ("Gegenschub : nach links");
            }
        }
    }

    void Leftward ()
    {
        if (leftward) {
            if (right > -100) {
                rb.AddForce (transform.right * -1 * 5, ForceMode.Acceleration);
                right--;
            }
        } else {
            if (right < 0) {
                rb.AddForce (transform.right * 5, ForceMode.Acceleration);
                right++;
                Debug.Log ("Gegenschub : nach rechts");
            }
        }
    }
}

double click the debug.log that says false and see if it goes to that Debug.Log(leftward);

If it does, create a new script with just the If(Input.GetKey(KeyCode.A)) and see if it still shows it being false and true.

1 Like

After changing my keyboard twice, it worked.
Thanks for your help anyways!