- Home /
having a dilemma with a canShoot bool in an fps
Hey all,
I'm having a tricky issue here that I got myself into, and I've been trying to wrap my head around it for hours, but feel like I've given myself tunnel vision now, so I could use some fresh eyes. Here's my code:
https://wtools.io/paste-code/b2R8
Basically, I have a setup where my weapon uses a bool to check if it can shoot or not, and I run a coroutine in between shots to give it a "shotgun" feel. When I implemented a pause menu, I had to try and disable my shooting during pause. Problem is, I put the canShoot bool = true (if not paused) in update. So either my shotgun now fires for every click (because it's always true) OR it doesnt fire at all after pausing. I feel like there is either an easy fix or I have to really re do this system. Any ideas?
Answer by Llama_w_2Ls · Dec 09, 2020 at 09:37 PM
Instead of disabling your gun on pause, disable the UI Buttons or key presses that can shoot, or change them to something that can't be pressed. This will prevent any hassle or debugging issues later.
Answer by highpockets · Dec 09, 2020 at 09:38 PM
Why don’t you setup some events instead of checking every update call?? Pause game can Invoke() an event that tells listeners it is not paused and you only set your shooting bool true when the event is triggered not every update.
[System.Serializable]
public class MyBoolEvent : UnityEvent<bool>
{}
public class PauseClass : MonoBehaviour
{
public MyBoolEvent isPausedEvent;
[SerializeField] private Button pauseButton;
private bool isPaused = false;
private void Start()
{
if(isPausedEvent == null)
isPausedEvent = new MyBoolEvent();
pauseButton.onClick.AddListener( ( ) =>
{
isPaused = !isPaused;
isPausedEvent.Invoke( isPaused );
} );
}
}
public class ShotgunClass : MonoBehaviour
{
[SerializeField] private PauseClass;
private bool canShoot = true;
private void Start()
{
PauseClass.isPauseEvent.AddListener((bool pauseStatus) =>
{
if(pauseStatus)
canShoot = false;
else
canShoot = true;
} );
}
}
Answer by logicandchaos · Dec 10, 2020 at 12:43 PM
You should set it up so none of the code for your player runs while the game is paused.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
(c#) Problems with yield 1 Answer
I am trying to create a leaning mechanic for an FPS game but I have one problem 1 Answer