How to make character one time shoot in one direction ?

Hey everyone,

I design a character can shoot in four direction: up down left right. By using four button: W S A D. The issue is that , when i push those buttons together the character can shoot in direction at same. But what i want is when i push one button the character only can shoot in that direction .I don`t want my character can shot in different directions at same time. How can i fix this.Please assist .
This is my code:

1896013–122103–Gun.cs (1.09 KB)

One solution could be to use else if (instead of 4 distinct if statements). The user must release the current button to be able to shoot into another direction.
e.g.

        if(Input.GetButtonDown("FireRight"))
        {
           Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
            bulletInstance.velocity = new Vector2(speed, 0);
        }      
           else if(Input.GetButtonDown("FireLeft"))
        {
            Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,180))) as Rigidbody2D;
            bulletInstance.velocity = new Vector2(-speed, 0);
        }
        else if(Input.GetButtonDown("FireUp"))
        {
            Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,90))) as Rigidbody2D;
            bulletInstance.velocity = new Vector2(0, speed);
        }
        else if(Input.GetButtonDown("FireDown"))
        {
            Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,270))) as Rigidbody2D;
            bulletInstance.velocity = new Vector2(0, -speed);
        }

Thanks for ur adivce. I tried ur code but it still can shoot different directions at same time.But ur idea is helpful. So i can make my button like this: disablle other three buttons when i push one.and active those button after i released. But i don`t know the codes which can disable and reactive buttons. If u know , could u teach me that. XD

Could you try

Input.GetKey

instead of

Input.GetButtonDown?

THX GetKey can …But the frequency is too short …i try to add some time delay …Thanks of ur help~XD

as much as I hate code duplication… a remake of your current script with some fire delay:

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour {
    public Rigidbody2D bullet;           
    public float speed = 20f;
    public float fireDelay = 0.5f;
    private bool canFire = true;
   
    void Update()
    { 
        if(Input.GetButtonDown("FireRight") && (canFire))
        {
            Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
            bulletInstance.velocity = new Vector2(speed, 0);
            canFire = false;
            Invoke("ToggleFire", fireDelay);
        }

        if (Input.GetButtonDown("FireLeft") && (canFire))
        {
            Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,180))) as Rigidbody2D;
            bulletInstance.velocity = new Vector2(-speed, 0);
            canFire = false;
            Invoke("ToggleFire", fireDelay);
        }

        if (Input.GetButtonDown("FireUp") && (canFire))
        {
            Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,90))) as Rigidbody2D;
            bulletInstance.velocity = new Vector2(0, speed);
            canFire = false;
            Invoke("ToggleFire", fireDelay);
        }

        if (Input.GetButtonDown("FireDown") && (canFire))
        {
            Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0,0,270))) as Rigidbody2D;
            bulletInstance.velocity = new Vector2(0, -speed);
            canFire = false;
            Invoke("ToggleFire", fireDelay);
        }           
    }

    void ToggleFire()
    {
        canFire = !canFire;
    }
}

Thank u very much!