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

Question How to Lock and Unlock Player's Input?

Discussion in 'Input System' started by JourneytoUngoro, Jan 16, 2024.

  1. JourneytoUngoro

    JourneytoUngoro

    Joined:
    Jul 10, 2023
    Posts:
    24
    I'm making a system to lock the player's input for a few seconds and release it.
    I'm using unity input system and its behavior is set to 'Invoke Unity Events'.
    It seems like that code below only reads when the player gives new input.

    Code (CSharp):
    1. public void OnMoveInput(InputAction.CallbackContext context)
    2. {
    3.     movementInput = context.ReadValue<Vector2>();
    4.     normInputX = preventInputX ? fixedInputX : Mathf.RoundToInt(movementInput.x);
    5.     normInputY = Mathf.RoundToInt(movementInput.y);
    6. }
    So the problem I'm having is when player's input is unlocked, unless the player presses the button again, normInputX is still set to fixedInputX and doesn't change.
    Is there a way to read button again that player is pressing by function call or should I just make my input system to read every frame?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,230
    I think some part of this is going to have to be polling on a per-frame basis.

    One thing you could do is check the phase of the input: https://docs.unity3d.com/Packages/c...UnityEngine.InputSystem.InputActionPhase.html

    So maybe when the user cancels input you start a timer for n seconds.

    Though you might be able to get more finer control by rather than using the PlayerInput component, instead hooking into directly with your InputAction's callbacks, or simply polling the inputs on a per-frame basis (it's not as bad as some folks make it out to be).
     
  3. JourneytoUngoro

    JourneytoUngoro

    Joined:
    Jul 10, 2023
    Posts:
    24
    I totally just forgot about this thread and currently I wrote a code like below. I'm sure that there will be a much better way than what I did below, but as for me, it was my best effort.

    Code (CSharp):
    1. public Vector2 movementInput { get; private set; }
    2. public int normInputX { get; private set; }
    3. public int normInputY { get; private set; }
    4. public int actualInputX { get; private set; }
    5. public int fixedInputX { get; private set; }
    6. public bool preventInputX { get; private set; }
    7.  
    8. public void OnMoveInput(InputAction.CallbackContext context)
    9. {
    10.     movementInput = context.ReadValue<Vector2>();
    11.     actualInputX = Mathf.RoundToInt(movementInput.x);
    12.     normInputX = preventInputX ? fixedInputX : actualInputX;
    13.     normInputY = Mathf.RoundToInt(movementInput.y);
    14. }
    15.  
    16. public void PreventInputX(int facingDirection)
    17. {
    18.     if (preventInputX == false)
    19.     {
    20.         fixedInputX = facingDirection;
    21.         normInputX = facingDirection;
    22.         preventInputX = true;
    23.     }
    24. }
    25.  
    26. public void AvailInputX()
    27. {
    28.     if (preventInputX)
    29.     {
    30.         preventInputX = false;
    31.         normInputX = actualInputX;
    32.     }
    33. }
    ...By the way, how can I change my tag from Question to Solved?