0
\$\begingroup\$

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;
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ You can store a flag "GameWasPaused" that only gets cleared one or two frames after the game unpaused. In the Shooting class, you can then check whether this flag is set, and if yes, ignore the mouse click. \$\endgroup\$ – Chillersanim Aug 13 '19 at 19:07
2
\$\begingroup\$

I guess a possible solution is to have a bool that handles if the player is allowed to shoot.

public class Shooting : MonoBehaviour
{
    /// ....
    public bool AllowedToShoot = true;

    void Update()
    {
        if (AllowedToShoot == false) return; // if not allowed to shoot, don't run the code bellow
        // your code
    }
}


public class PauseMenu : MonoBehaviour
{
    // ...

    public Shooting ShooterAgent; // this must not be null in the editor

    void Resume()
    {
        /// ...
        ShooterAgent.AllowedToShoot = true;
    }

    void Pause()
    {
        /// ...
        ShooterAgent.AllowedToShoot = false;

    }
}

You can expand it to only call a method instead of accessing and changing the bool directly.

public void AllowShooting(bool status)
{
    AllowedToShoot = status;
}
\$\endgroup\$
  • \$\begingroup\$ This works super well. I had to make some adjustments to make it also work for stopping the shooting animation properly. I anyone has this issue this is definitely the right answer. \$\endgroup\$ – Ethan K-B Aug 14 '19 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.