Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to detect that a button is already held when a PlayerInput is instantiated?

Discussion in 'Input System' started by tmcdonald, May 24, 2020.

  1. tmcdonald

    tmcdonald

    Joined:
    Mar 20, 2016
    Posts:
    160
    I have a PlayerInput on a player prefab. I have a button that the player holds to run. That works fine when the button is pressed after the prefab is instantiated. But if the player is already holding the button when the prefab is instantiated, I receive no event calls. Which makes sense - they didn't "just press" the button. But as far as I know, there's no way to just check the Player Input control state, is there? Is the correct way to approach this problem to just avoid using the Player Input component? Or is there a way to solve it using that component?

    The specific gameplay example: It's a platformer game. The player is running around, falls into a lava pit. The level resets itself, deleting the old player instance and instantiating a new one. The player prefab has a Player Input component on it communicating with the player controller. If the player continues to hold the "run" button after dying, they are no longer running until they release and re-press the button.

    Let me know if more info is needed.
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Short answer: Switching the action type from "Button" to "Value" should do the trick.

    Long answer: there's an assumption built into this. Button behavior is expected to revolve around presses and releases whereas "value" behavior is taken you simply want the input to assume the current value of the control -- whatever that is. So, unlike a button-type action, a value-type action will immediately go and check the current control value(s) when the action is enabled so that if a control is already actuated when the action is enabled, the action immediately triggers.
     
  3. tmcdonald

    tmcdonald

    Joined:
    Mar 20, 2016
    Posts:
    160
    Thanks! That did the trick. I can see that the action is properly cancelled when the Player Input is destroyed, and re-performed when it gets reinstantiated.