0
votes

I'm making a top down shooter game in unity using the old input system and I ran into a problem. I use the arrow keys to shoot but I'm not sure how to fire in multiple directions at the same time/frame. Code:

void Update()
    {
        currentTime += Time.deltaTime;
        //input section
        if (Input.GetKey(KeyCode.UpArrow))
        {
            ShootUp();
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            ShootDown();
        }

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            ShootLeft();
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
            ShootRight();
        }

        if (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))
        {
            ShootUp();
            ShootRight();
        }

 }

I thought that looking at both arrow keys at the same time, I would just call both functions as well. Its does not work however. I tried google but it's pretty hard to find info on the old input system having the same problem.

1
I see nothing wrong with your code. I just tossed it into my update and logged the results and it notified me of every key press or combination of keypresses. Try logging your results too. Also, you're going to shoot 4 times if you check "if (Input.GetKey(KeyCode.UpArrow))" with if (Input.GetKey(KeyCode.RightArrow)) and "if (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))" That may be causing your issue - Display name
Your last if has no reason to exist. If UpArrow and RightArrow are down, it should already enter their respective if-scopes. Could you add what actually happens in ShootUp and ShootRight? To make sanity checks, add log-statements within the ifs and/or use the debugger to step through the code. - Max Play

1 Answers

-2
votes

Okay, so first of all. All of these should go into a seperate method and then called in Update. Secondly, Input.GetAxis use that, and write a method which would take a Vector2 direction and instantiate a bullet towards that direction.


private void Update()
{
   currentTime += Time.deltaTime;
   Shooting();
}

private void Shooting()
{
   var x = Input.GetAxis("Horizontal");
   var y = Input.GetAxis("Vertical");
   Vector2 direction = new Vector2(x, y);
   
}

private void Shoot(Vector2 direction)
{
   // Instantiate bullets and fire them towards direction.
}

Google Input.GetAxis method, and Vector2 in case you do not know that.