• 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 /
avatar image
1
Question by absurdnoize · May 12, 2020 at 10:29 AM · inputgamepadinputsinput setting

New Unity Input System Getting continuous input

When I press the button the actions performs only once. How can I change it so as long as the button is being hold the action would go on?

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by cassidyg · Nov 26, 2020 at 03:47 AM

@absurdnoize The way I handled t$$anonymous$$s was with a variable. If you are using the Input System, you would need to do a few t$$anonymous$$ngs:

  • In your Input Asset, add a Hold interaction to your button

  • Create 2 methods in w$$anonymous$$chever script you are using to receive input

  • 1 method should be subscribed to your button's performed action and 1 method should be subscribed to the canceled action.

If you are not using the input system, you can follow the same process, but with the old input system, so no need to subscribe delegates to your button actions.

     ...
     using UnityEngine.InputSystem;
     using UnityEngine.InputSystem.Interactions;
     
     public class PlayerController : MonoBehaviour
     {
         private bool isAttackHeld;
 
         // How many seconds your coroutine will wait before doing its loop again
         private float delay = .1f;
 
         // When I created my input asset, I exported it as a class so I could
         reference it in other files, so your name may be different from mine
         private InputConfig _inputConfig = new InputConfig();
     
         // Subscribe your methods to the performed & canceled actions on enable
         void OnEnable() {
             _inputConfig.PlayerMap.Attack.performed += handleAttack;
             _inputConfig.PlayerMap.Attack.canceled += handleAttackCancel;
         }
     
         // Unsubscribe your methods to the performed & canceled actions on disable
         void OnDisable() {
             _inputConfig.PlayerMap.Attack.performed -= handleAttack;
             _inputConfig.PlayerMap.Attack.canceled -= handleAttackCancel;
         }
     
         IEnumerator AttackHoldCo() {
             w$$anonymous$$le (isAttackHeld) {
                 // Do whatever you need to do
                 yield return new WaitForSeconds(delay);
     
                 // can also be 'yield return null;' if you want t$$anonymous$$s to loop every frame
                 // but that can be taxing on some computers, depending on what you are doing
             }
         }
     
         // T$$anonymous$$s gets called when the action is performed
         // In t$$anonymous$$s case, that means t$$anonymous$$s gets called both when the button is
         // pressed & when the button is held down
         void handleAttack() {
                 isAttackHeld = true;
                 // Do whatever needs to be done if the button is held down
                 StartCoroutine(AttackHoldCo());
             }
         }
     
         // T$$anonymous$$s gets called when the action is canceled
         // In t$$anonymous$$s case, that is when we release the button
         void handleAttackCancel() {
             if (isAttackHeld)
             {
                 isAttackHeld = false;
                 // Anyt$$anonymous$$ng else you need to do upon canceling your action
                 EndAttack(); // T$$anonymous$$s can be anyt$$anonymous$$ng, doesn't matter for t$$anonymous$$s explanation
             }
         }
     }


I have mine set up to handle both a press & a hold on the same button, w$$anonymous$$ch is why in handleAttackCancel() I first check to see if I am holding the button down before calling my EndAttack() method. If you don't care about that, then you don't need that if statement.

Comment
Add comment · Show 1 · 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 liuds3 · Oct 09, 2022 at 01:56 PM 0
Share

Hey, I'm trying to do a continous shooting system, but when I press the button once it would only shoot once. And if a button is pressed in between attacks, when the attackDelay would end it would shoot again. I managed to do it, but it seems insufficient with four booleans. Maybe you have some suggestions to making it better? Would appreciate any help.

 public class PlayerAttack : MonoBehaviour
 {
     [SerializeField] private float PistolAttackDelay = .5f;
     [SerializeField] private GameObject Bullet;
     [SerializeField]  private Transform BulletSpawnPoint;
     private bool CanAttack = true;
     private bool isAttackHeld;
     private bool AttackPressed = false;
     private bool AttackReleased = false;
     //When mouse button is pressed this method is called
     public void PistolAttack()
     {
         if (CanAttack)
         {
             isAttackHeld = true;
             StartCoroutine(ShootBullet());
         }
         else
         {
             AttackPressed = true;
             isAttackHeld = true;
         }
     }
     IEnumerator ShootBullet()
     {
         CanAttack = false;
         while (isAttackHeld)
         {
             Instantiate(Bullet, BulletSpawnPoint);
             yield return new WaitForSeconds(PistolAttackDelay);
             if (AttackReleased)
             {
                 AttackReleased = false;
                 break;
             }
         }
         if (AttackPressed)
         {
             AttackPressed = false;
             if (!isAttackHeld)
                 AttackReleased = true;
             isAttackHeld = true;
             StartCoroutine(ShootBullet());
         }
         else
             CanAttack = true;
     }
     public void CancelAttack()
     {
         isAttackHeld = false;
     }
 }


Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

155 People are following this question.

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

Related Questions

New Input System: Can't set binding path anymore 2 Answers

How to detect input being hold in new Input System 3 Answers

How do I fix unity game not working correctly with a controller if it is plugged in before the game starts? 0 Answers

Input - is it OK to use SWITCH? 2 Answers

Creating a menu for gamepad? 1 Answer


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