Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to continuously call an action?

Discussion in 'Input System' started by FeastSC2, Jun 8, 2021.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I use the directional arrows to read input to move my player.

    But sometimes, when I minimize the game or for other weird reasons I don't quite know why the function doesn't store the fact that I have stopped pressing the directional arrows and then the player keeps moving.

    I would like to constantly/continuously call the "Move" action so that the input is read every frame.
    Is that possible to do with the Input Package System (new input system)?

    upload_2021-6-8_19-2-9.png
     
  2. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    262
    You can call .ReadValue on the Action if you reference it directly. In my code, I have a handle on the action and then say
    var stick = moveAction.ReadValue<Vector2>();

    To get a handle on an Action, you keep a field for the InputActionsAsset, and assign it in the inspector. Then in Awake on your input script say

    Code (CSharp):
    1. private void Awake()
    2. {
    3.   myInputActionsAsset = Instantiate(inputActionsAsset);
    4.   moveAction = myInputActionsAsset["Move"];
    5. ....
    Mind you, I'm not using the PlayerInput script that Unity provides. I am just getting back into this project after 6 months away and at the time, PlayerInput didn't work out... but I cannot recally why. :)
     
    FeastSC2 likes this.
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Yea I did the same, I think it's better to not use the PlayerInput :)

    When your class that handles input inherits from the interface of your Controls, it's easier to set it up like that Controls.IPlayerActions than with PlayerInput.
     
  4. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    262
    Yeah I actually found the entire system to be elegant and simple when avoiding PlayerInput.

    Did my solution make sense for you?
     
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    yes it seems to work, thanks!
     
    benthroop likes this.
  6. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978