0
votes

So I am creating my first game in unity and I am currently working on the left to right movement.

  • I move to the left, let go of the left arrow button -> idle animation.

  • Press right arrow and character moves to right, let go -> idle.

The problem is I have to wait for the idle animation before I can press to go to the other direction chosen. When I press left and same time right the character does not move.

What I would like to do is fe:

  • press left -> character goes left, and then

  • press right at the same time -> character goes right.

So the need to be able to press buttons at the same time, and the last key pressed dictates the movement/animation.

Animation code:

if (Input.GetKeyDown(KeyCode.LeftArrow)) 
{
    this.GetComponent<Animator>().SetInteger("pallotila", 1);
}

if (Input.GetKeyUp(KeyCode.LeftArrow))
{
    this.GetComponent<Animator>().SetInteger("pallotila", 0);
}

if (Input.GetKeyDown(KeyCode.RightArrow))
{
    this.GetComponent<Animator>().SetInteger("pallotila", 2);
}

if (Input.GetKeyUp(KeyCode.RightArrow))
{
    this.GetComponent<Animator>().SetInteger("pallotila", 0);
}

Player movement code:

if (Input.GetKey(KeyCode.LeftArrow))
{
    this.transform.position += Vector3.left * this.nopeus * Time.deltaTime;
    this.transform.rotation = this.vasemmalle;
}

if (Input.GetKey(KeyCode.RightArrow))
{
    this.transform.position += Vector3.right * this.nopeus * Time.deltaTime;
    this.transform.rotation = this.oikealle;
}
2

2 Answers

0
votes

Note that you should never use GetComponent in Update better do it once and reuse the reference.

//Here you store the Animator reference
private Animator animator;

private void Awake()
{
    animator = GetComponent<Animator>();
}

I'ld also use a switch to define what should happen for which button in order to avoid having the same code over and over again.

private void SetLastPressed(KeyCode code)
{
    int value = 0;
    switch (code)
    {
        case KeyCode.None:
            value = 0;
            break;

        case KeyCode.LeftArrow:
            value = 1;
            break;

        case KeyCode.RightArrow:
            value = 2;
            break;
    }

    animator.SetInteger("pallotila", value);
    lastPressed = code;
}

Simply store and check which button pressed last and make the Input checks exclusive using if-else.

// Here you store the last pressed key
private KeyCode lastPressed = KeyCode.None;

private void Update()
{

    if (lastPressed != KeyCode.LeftArrow && Input.GetKeyDown(KeyCode.LeftArrow))
    {
        SetLastPressed(KeyCode.LeftArrow);
    }
    else if (lastPressed != KeyCode.RightArrow && Input.GetKeyDown(KeyCode.RightArrow))
    {
        SetLastPressed(KeyCode.RightArrow);
    }

    // If none of the keys is pressed reset
    else if (lastPressed != KeyCode.None && !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow))
    {
        SetLastPressed(KeyCode.None);
    }

    // And if only one of them is released but the other one still pressed
    //go on using that still pressed key again
    else if (lastPressed != KeyCode.LeftArrow && Input.GetKeyUp(KeyCode.RightArrow) &&
             Input.GetKey(KeyCode.LeftArrow))
    {
        SetLastPressed(KeyCode.LeftArrow);
    }
    else if (lastPressed != KeyCode.RightArrow && Input.GetKeyUp(KeyCode.LeftArrow) &&
             Input.GetKey(KeyCode.RightArrow))
    {
        SetLastPressed(KeyCode.RightArrow);
    }

For the movement you could simply reuse the lastPresses value than as well

    if(lastPressed == KeyCode.LeftArrow)
    {
        transform.position += Vector3.left * nopeus * Time.deltaTime;
        transform.rotation = vasemmalle;
    }
    else if (lastPressed == KeyCode.RightArrow)
    {
        transform.position += Vector3.right * nopeus * Time.deltaTime;
        transform.rotation = oikealle;
    }

Additionally you could/should use a List<KeyCode> to store the last presses. Everytime a button goes down add the button to the end of the list; everytime a button goes up remove it from the list

result => the last pressed button is always the last one in the list.

This makes the return to a previous button way more simple and flexible without adding/changing so much lines of code like

private List<KeyCode> lastPresses = new List<KeyCode>();
private KeyCode lastPressed = KeyCode.None;

private Animator animator;

private void Awake()
{
    animator = GetComponent<Animator>();
}

private void SetLastPressed(KeyCode code)
{
    int value = 0;
    switch (code)
    {
        case KeyCode.None:
            value = 0;
            break;

        case KeyCode.LeftArrow:
            value = 1;
            break;

        case KeyCode.RightArrow:
            value = 2;
            break;
    }

    animator.SetInteger("pallotila", value);
    lastPressed = code;
}

private void Update()
{
    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
        if (!lastPresses.Contains(KeyCode.LeftArrow)) lastPresses.Add(KeyCode.LeftArrow);
    }
    else if (Input.GetKeyUp(KeyCode.LeftArrow))
    {
        if (lastPresses.Contains(KeyCode.LeftArrow)) lastPresses.Remove(KeyCode.LeftArrow);
    }

    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        if (!lastPresses.Contains(KeyCode.RightArrow)) lastPresses.Add(KeyCode.RightArrow);
    }
    else if (Input.GetKeyUp(KeyCode.RightArrow))
    {
        if (lastPresses.Contains(KeyCode.RightArrow)) lastPresses.Remove(KeyCode.RightArrow);
    }

    var currentCode = lastPresses.Count > 0 ? lastPresses[lastPresses.Count - 1] : KeyCode.None;

    if (currentCode != lastPressed) SetLastPressed(currentCode);

    if (lastPressed == KeyCode.LeftArrow)
    {
        transform.position += Vector3.left * nopeus * Time.deltaTime;
        transform.rotation = vasemmalle;
    }
    else if (lastPressed == KeyCode.RightArrow)
    {
        transform.position += Vector3.right * nopeus * Time.deltaTime;
        transform.rotation = oikealle;
    }
}
0
votes

You can change the "Value" Variable to

    animator.SetInteger("pallotila", 0);
    break;

in the location of

value = 0;
break;

or change

int value;

to

int value = 0;

in the code by @derHugo