Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Using new input system, how to setup a "Hold spacebar for 3 or more seconds, then do something"

Discussion in 'Input System' started by MegaTDog, Apr 1, 2020.

  1. MegaTDog

    MegaTDog

    Joined:
    Jun 30, 2019
    Posts:
    8
    Hi,
    I am using the new input system which has simplified alot of my scripts.

    Im trying to make an attack in my game, where if the player holds down spacebar for 3 or more seconds, their attack will increase and do more damage (heavy attack) once they let go of space bar. If they let go of spacebar before 3 seconds, they do a weaker (light attack)

    I have set up my Input Actions to have an Attack action with the Space key having a hold of 3 (i assume this means 3 seconds)
    upload_2020-4-1_21-45-17.png

    How would this be done in code? I have a pseudocode below
    Code (CSharp):
    1.  
    2. OnAttack()
    3. {
    4.  
    5. if(spacebar is held for >= 3)
    6. {
    7. heavyAttack();
    8. }
    9. else
    10. {
    11. lightAttack();
    12. }
    13.  
    14. }
    15.  
    Thank you for your help
    MegaTDog
    (Anthony)
     
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    In case you are using PlayerInput: ATM you will need to use the "Invoke Unity Events" or "Invoke CSharp Events" behavior. The SendMessages/BroadcastMessages uses a simplified callback model where you don't get all the callbacks.

    As for the code:

    Code (CSharp):
    1. void OnAttack(InputAction.CallbackContext context)
    2. {
    3.     switch (context.phase)
    4.     {
    5.         case InputActionPhase.Started:
    6.             /* the space key was pressed */
    7.             break;
    8.  
    9.         case InputActionPhase.Performed:
    10.             /* the space key was held for 3 seconds */
    11.             break;
    12.  
    13.         case InputActionPhase.Canceled:
    14.             /* the space key was released; if there was no 'Performed', it was released before 3 seconds were up */
    15.             break;
    16.     }
    17. }
    18.  
     
  3. MegaTDog

    MegaTDog

    Joined:
    Jun 30, 2019
    Posts:
    8
    Hi Rene-Damn,
    Thanks for the reply.

    It seems I can't get unity to register my space key input at all.

    I have changed the behaviour from Send Messages to Invoke C Sharp Event on my Player GameObject
    upload_2020-4-2_21-10-33.png

    Then I added the following code my playerController.cs script

    Code (CSharp):
    1.     void OnAttack(InputAction.CallbackContext context)
    2.     {
    3.         switch (context.phase)
    4.         {
    5.             case InputActionPhase.Started:
    6.                 /* the space key was pressed */
    7.                 Debug.Log("the space key was pressed");
    8.                 break;
    9.  
    10.             case InputActionPhase.Performed:
    11.                 /* the space key was held for 3 seconds */
    12.                 Debug.Log("the space key was held for 3 seconds");
    13.                 break;
    14.  
    15.             case InputActionPhase.Canceled:
    16.                 /* the space key was released; if there was no 'Performed', it was released before 3 seconds were up */
    17.                 Debug.Log("the space key was released; if there was no 'Performed', it was released before 3 seconds were up");
    18.                 break;
    19.         }
    20.     }
    However, unity does seem to pick up that I am pressing space.

    Any idea what I am doing incorrectly?

    Cheers
     
  4. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Set it to "Invoke Unity Events". The C# events require explicitly hooking into the event from code.

    With "Invoke Unity Events", there will be an "Events" foldout in the PlayerInput inspector. Unfold it and hook up the event for your action to the method you want called.
     
  5. MegaTDog

    MegaTDog

    Joined:
    Jun 30, 2019
    Posts:
    8
    Hi Rene-Damn,

    Aplogies, I don't quite understand how to use the Invoke Unity Event.

    What do I add here?
    upload_2020-4-4_13-58-1.png

    Is it the playerController.cs script that has the onAttack() function or do I need to add something else?

    I apologies if this is a dumb question. My experience with C# and Unity is still relatively basic

    Thank you
     
  6. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    No worries.

    Unity events allow you to drag a reference to an object in there and then select a method to invoke on one of the components of the object when the event is triggered.

    Drag the GameObject that has the playerController.cs script on it into the field that says "None (O...". The dropdown that says "No Function" will become enabled. Open it, go into your playerController component and select the OnAttack method. No you've established that when the "Attack" event triggers, your OnAttack method will be called.
     
  7. MegaTDog

    MegaTDog

    Joined:
    Jun 30, 2019
    Posts:
    8
    So i dragged the "Player" object that has the playerMovement.cs attached to it into the Player Input Script but I can't find my OnAttack method in any of the dropdown boxes that it provides in the screenshot below:
    upload_2020-4-9_15-11-1.png

    The Player Input script is also attached to my "Player" game object. Is this a problem?