When I exit the pause menu of my game the gun fires

by Ethan K-B   Last Updated August 13, 2019 19:13 PM

When The player exits the pause menu in my game the gun will fire. I figured out that it happens when the player clicks when the game is paused.

Basically i the player goes to the pause menu and exits without clicking the gun will not fire, but if they go to the pause menu and click then exit the gun will fire. Here are my scripts:

public class Shooting : MonoBehaviour
{
    Animator anim;
    public float FireRate = 15f;
    private float NextTimeToFire = 0f;
    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetButton("Fire1") && GetComponentInParent<PlayerController>().moving == false && Time.time >= NextTimeToFire)
        {
            NextTimeToFire = Time.time + 1f / FireRate;
            Shoot();
        }
    }
}

/

public class PauseMenu : MonoBehaviour
{
    public static bool GameIsPaused = false;
    public GameObject HUDPanel;
    public GameObject PauseMenuPanel;
    public GameObject OptionsPanel;

    private void Start()
    {
        Time.timeScale = 1f;
        GameIsPaused = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (GameIsPaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }

    void Resume()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        PauseMenuPanel.SetActive(false);
        OptionsPanel.SetActive(false);
        HUDPanel.SetActive(true);
        Time.timeScale = 1f;
        GameIsPaused = false;
    }

    void Pause()
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
        PauseMenuPanel.SetActive(true);
        HUDPanel.SetActive(false);
        Time.timeScale = 0f;
        GameIsPaused = true;
    }
}
Tags : unity c# pausing


Related Questions


Updated September 11, 2016 08:05 AM

Updated November 15, 2017 12:13 PM

Updated May 26, 2016 08:05 AM

Updated May 24, 2017 14:13 PM