- Home /
The question is answered, right answer was accepted
Stop checking for key input
Hello all. In the game I'm helping develop, we are trying to make an ammo system, but there's one problem. I have no idea how I would start and stop checking for input when a certain condition is met. Say I wanted to stop checking for input because they ran out of clips of ammo or because they need to reload. I was thinking that I could make a boolean (In the code you'll see that I did, it's called "CanShoot") and then say that boolean is false but I don't know how to connect the dots. Thank you for your help.
if ((Input.GetButton("Fire1") && (M4A1.activeSelf || M9.activeSelf) && Time.time > nextFire)) //Allowing the user to hold down or single click the mouse button.
{
if (M4A1.activeSelf || M9.activeSelf && Input.GetButton("Fire1"))
{
if (M4A1.activeSelf)
{
M4A1_Ammo.gameObject.SetActive(true);
M9_Ammo.gameObject.SetActive(false);
CurrentM4A1Ammo -= 1;
Vector3 M4A1BulletPosition = new Vector3(-0.232f, 0.3f, -0.067f);
Instantiate(M4A1Bullet, BulletPosition, Quaternion.identity);
FinalM4A1Ammo = CurrentM4A1Ammo;
M4A1_Ammo.value = CurrentM4A1Ammo;
print("M4A1 ammo is " + CurrentM4A1Ammo);
if (CurrentM4A1Ammo == 0)
{
CanShoot = false;
M4A1Clips -= 1;
print("M4A1 clips: " + M4A1Clips);
M4A1_Ammo.value = CurrentM4A1Ammo;
if ((Input.GetButton("Fire1") && (M4A1.activeSelf || M9.activeSelf) && Time.time > nextFire))
{
if (M9.activeSelf)
{
M9_Ammo.gameObject.SetActive(true);
M9_Ammo.gameObject.SetActive(false);
CurrentM9Ammo -= 1;
// Instantiate(M4A1Bullet, BulletPosition, Quaternion.identity);
FinalM4A1Ammo = CurrentM4A1Ammo;
M4A1_Ammo.value = CurrentM4A1Ammo;
print("M9 ammo is " + CurrentM4A1Ammo);
}
}
if (M4A1Clips == 0)
{
// Disallow reload here.
// Disallow Fire1 here.
}
}
if (CanShoot == false)
{
// This is where you would disable checkforinput.
}
}
if (M9.activeSelf)
{
M9_Ammo.gameObject.SetActive(true);
M4A1_Ammo.gameObject.SetActive(false);
CurrentM9Ammo -= 1;
Vector3 M9BulletPosition = new Vector3(-0.232f, 0.3f, -0.067f);
// Instantiate(M4A1Bullet, BulletPosition, Quaternion.identity);
FinalM9Ammo = CurrentM9Ammo;
M9_Ammo.value = CurrentM9Ammo;
print("M9 ammo is " + CurrentM9Ammo);
if (CurrentM9Ammo == 0)
{
M9Clips -= 1;
print("M9 clips: " + M9Clips);
M9_Ammo.value = CurrentM9Ammo;
if ((Input.GetButton("Fire1") && (M4A1.activeSelf || M9.activeSelf) && Time.time > nextFire))
{
if (M9.activeSelf)
{
M9_Ammo.gameObject.SetActive(true);
M4A1_Ammo.gameObject.SetActive(false);
CurrentM9Ammo -= 1;
// Instantiate(M4A1Bullet, BulletPosition, Quaternion.identity);
FinalM9Ammo = CurrentM9Ammo;
M9_Ammo.value = CurrentM9Ammo;
print("M9 ammo is " + CurrentM9Ammo);
}
}
if (M9Clips == 0)
{
// Disallow reload here.
// Disable fire1 here.
}
}
}
Answer by RobAnthem · Feb 17, 2017 at 07:03 PM
It should be as simple as making a bool and encapsulating your input check inside of an if statement like so.
public bool canFire;
void Update()
{
if (canFire)
{
if ((Input.GetButton("Fire1") && (M4A1.activeSelf || M9.activeSelf) && Time.time > nextFire))
{
//Your firing method.
if (ammo <= 0)
{
canFire = false;
}
}
}
}
Is there anyway that you know of where I can stop checking for input of a function? Thank you.
As @RobAnthen stated, you couldmake use of a bool
so you can control when to check for an input. That canFire
stops or allows the Input check execution, when canFire == true
firing is enabled ergo the code the if
statement encapsulates becomes avaliable but when it is canFire == false
the code the if
statement encapsulates isn't avaliable.
if(canFire){ //Same as canFire == true
//The Input check goes here and only becomes accesible when canfire is true
}
Follow this Question
Related Questions
pick up ammo from specific gun? 1 Answer
My automatic gun don't work help! 1 Answer
How can I make an input field only take in numbers and decimals? 2 Answers
Unity 5 Transparent Terrain Shader 3 Answers