Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question What is the replacement for GetButtonUp?

Discussion in 'Input System' started by swanne, May 13, 2020.

  1. swanne

    swanne

    Joined:
    Dec 17, 2015
    Posts:
    10
    Hi,

    I am using the new input system and have no problems moving my character, jumping or attacking. The problem I have is that the moveSpeed defaults to walkSpeed value of 4f. I want to add a run feature which increases the moveSpeed to 6 whilst the run button is being pressed but as soon as it is released, return back to walkSpeed.

    All works fine except that when the runSpeed is applied, I dont know how to return it back to walkSpeed when the button is released.

    The way I am coding it is...

    1. private void OnEnable()
    2. {
    3. inputActions.Player.Enable();
    4. inputActions.Player.Jump.performed += Jump;
    5. inputActions.Player.Attack.performed += Attack;
    6. inputActions.Player.Run.performed += Run;
    7. }
    8. private void Run(InputAction.CallbackContext context)
    9. {
    10. moveSpeed = runSpeed;
    11. }
    The jump and attack work ok because they just use a trigger. I need the run to apply for as long as the button is pressed and then revert back to walkSpeed on release?

    Any help would be very much appreciated.
     
    TP3 likes this.
  2. fstorehaug

    fstorehaug

    Joined:
    Apr 3, 2017
    Posts:
    10
    maybe you could add a "press" interaction to your run action with "press and release" enabled. then you will get a canceled callback when the button is released.
     
  3. frarf

    frarf

    Joined:
    Nov 23, 2017
    Posts:
    27
    Use `inputActions.Player.Run.canceled`. Assuming your action is a button control then this will work as you intend.
     
  4. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    643
    The sample below if straight from my current project, IDK if thats the correct way to write it, but it works.
    PlayerInput is the autogenerated class from the new input system, "input" is a custom class with just those 9 variables and one method to set all of them to false.

    Code (CSharp):
    1.         PlayerInput controls;
    2.  
    3.  
    4.                     void PrepareAndSendInput()
    5.         {
    6.             input.ResetValues();
    7.  
    8. #if ENABLE_INPUT_SYSTEM
    9.             if (controls.Player.Fire.activeControl != null)
    10.             {
    11.                 ButtonControl button = controls.Player.Fire.activeControl as ButtonControl;
    12.                 input.shootButton = button.isPressed;
    13.                 input.shootButtonDown = button.wasPressedThisFrame;
    14.                 input.shootButtonUp = button.wasReleasedThisFrame;
    15.             }
    16.             if (controls.Player.AltFire.activeControl != null)
    17.             {
    18.                 ButtonControl button = controls.Player.AltFire.activeControl as ButtonControl;
    19.                 input.altShootButton = button.isPressed;
    20.                 input.altShootButtonDown = button.wasPressedThisFrame;
    21.                 input.altShootButtonUp = button.wasReleasedThisFrame;
    22.             }
    23.             if (controls.Player.Reload.activeControl != null)
    24.             {
    25.                 ButtonControl button = controls.Player.Reload.activeControl as ButtonControl;
    26.                 input.reloadButton = button.isPressed;
    27.                 input.reloadButtonDown = button.wasPressedThisFrame;
    28.                 input.reloadButtonUp = button.wasReleasedThisFrame;
    29.             }
    30. #else
    31. // old input code here
    32. #endif
    33. }