Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 30, 2019 at 12:37 PM by Sneakyshot4012 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Sneakyshot4012 · Feb 17, 2017 at 09:15 AM · disableammoguns

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.
                                 }
                             }
                         }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
3

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;
          }
       }
    }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sneakyshot4012 · Feb 20, 2017 at 02:37 AM 0
Share

Is there anyway that you know of where I can stop checking for input of a function? Thank you.

avatar image Patxiku Sneakyshot4012 · Feb 21, 2017 at 08:38 AM 0
Share

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

pick up ammo from specific gun? 1 Answer

my son is doing a multiplayer fps and is having a problem when each player picks up a gun and when one player shoots the other players gun fires to 4 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges