Preventing Precedence in Your Code

I might have a slight perfectionist problem and it bothered me that ‘A’ was taking precedence in the code, so I thought of a fairly simple way to prevent that from happening.

    if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.D) == false)
    {
        print("Rotating Left");
    }
    else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A) == false)
    {
        print("Rotating Right");   
    }

This way you don’t really add a single line of code, but provide a smoother experience to the player (I’m not sure how many people would actually get frustrated over the precedence thing tbh though)

1 Like

It bothered me as well so I googled what the other logical operators were in C# and specifically the ‘not’ operator, my code looks like this

     if ((Input.GetKey(KeyCode.A )) && (!(Input.GetKey(KeyCode.D))))
    {
        print("Rotating left");
    }
    else if ((Input.GetKey(KeyCode.D)) && (!(Input.GetKey(KeyCode.A))))
    {
        print("Rotating right");
    }

There’s a few more brackets in that than strictly necessary, but ignoring that, it’s doing basically the same thing as your code, in that it’s checking that the other key is not being pressed.

1 Like

Privacy & Terms