How to find out which one first pressed?

I added go up: if left and right movement pressed same time, but how to figure out which direction button is pressed first I want to go in that direction, it only goes on what command you have on first if method if it is left it goes left when you pressed both keys at same time; how to get first pressed key on keyboard when pressed right and left buttons pressed same time?

You can’t really know if they were pressed in the same frame. According to the game, both were pressed at the same time, there is no first.
If, however, you pressed one button and held it, and then pressed another you can know because one would already be pressed. Just ignore all others until the button is released again.
Maybe something like this

bool keyDown = false;

void Update()
{
    if (keyDown)
    {
        return;
    }

    if (Input.GetKeyDown(KeyCode.A))
    {
        // Do left movement
        keyDown = true;
    }
    else if (Input.GetKeyDown(KeyCode.D))
    {
        // Do right movement
        keyDown = true;
    }

    if (!Input.anyKey)
    {
        keyDown = false;
    }
}

Okay, thank you.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms