Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
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 this was with a variable. If you are using the Input System, you would need to do a few things:

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

  • Create 2 methods in whichever 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() {
             while (isAttackHeld) {
                 // Do whatever you need to do
                 yield return new WaitForSeconds(delay);
     
                 // can also be 'yield return null;' if you want this to loop every frame
                 // but that can be taxing on some computers, depending on what you are doing
             }
         }
     
         // This gets called when the action is performed
         // In this case, that means this 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());
             }
         }
     
         // This gets called when the action is canceled
         // In this case, that is when we release the button
         void handleAttackCancel() {
             if (isAttackHeld)
             {
                 isAttackHeld = false;
                 // Anything else you need to do upon canceling your action
                 EndAttack(); // This can be anything, doesn't matter for this explanation
             }
         }
     }


I have mine set up to handle both a press & a hold on the same button, which 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 · 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

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.

Follow this Question

Answers Answers and Comments

154 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

Related Questions

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

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

Dualshock3 Mac L2/R2 Trigger Analog Values? 1 Answer

Input System : Character movemevnt and Jump 1 Answer

New Input System : Changing control scheme sets an input to zero 0 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