Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

New Input System - Force the release of button press (Unity) (Help)

A topic by K-hunter87 created May 23, 2023 Views: 853 Replies: 5
Viewing posts 1 to 6
(1 edit)

I am very new to coding, please bare with me if I post something wrong. 

The player is able manually pick up and hold an item. While the pick up animation is playing, the move controls are temporarily set to zero velocity, and then set back to the OnMove controls. If the player holds any of the move buttons before the WaitForSeconds has switched control back, the player will not move until they release the key(s) they are holding.

I would like to force the release of the movement buttons in the script so the player doesn't have to release the button to start moving. Totally a quality of life fix but a necessary one. 

Thanks in advance!!!

(I can post more code if needed. Not 100% certain on how to post it, it doesn't seem to be as straight forward as posting an image or table, and it took me a while to find the "```" method. )


-Kyle


```
void OnMove(InputValue value)
     {
         if (canMove)
         {
             moveInput = value.Get<vector2>();
         }
         else
         {
             rb.velocity = new Vector2(0, 0);
         }
     }
     void OnJump(InputValue value)
     {
        if (!feetCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
         
        if(value.isPressed)
        {
             rb.velocity += new Vector2(0f, jumpSpeed);
        }
        anim.SetBool("isJumping", inAir);
     }
     void OnFire()
     {
         if (!playerHasHorizontalSpeed)
         {
             anim.SetTrigger("bend");
             canMove = false;
             StartCoroutine(WaitForBendAnim());
         }
     }
     IEnumerator WaitForBendAnim()
     {
         yield return new WaitForSeconds(0.6666666f);
         canMove = true;
     }
 }
Moderator moved this topic to General Development
Moderator

Just to make it clear, this is Unity, right?

Yea, it's unity. My bad for not posting that. >_<;;

Generally the way to force player input is to use another variable. For example:

if (Input.GetKeyUp(KeyCode.A) || ForceReleaseKey == true) {
    
}

I have a variable that locks the movement. If the player presses and holds a move button while locked, the code makes the velocity (0,0). When the variable unlocks it, its keeping the velocity at (0,0) until the button is released. Then the movement works the way it should. I want the player to start moving the second the animation finishes, without having to manually release the movement buttons. 

Instead of changing the rigidbody's velocity, is there a way to disable the input system's action for WASD? Or should I make a separate action map without WASD for when movement is supposed to be frozen and then switch back?

A simple solution:
In the OnMove method always store the last input value:

void OnMove(InputValue value)
{
    m_lastMove = value.Get<vector2>();
.
.
.

then when you set canMove to true, also update the movement to the last set value:

IEnumerator WaitForBendAnim()
{          
    yield return new WaitForSeconds(0.6666666f);
    canMove = true;
    moveInput = m_lastMove;
}

If you have more complex state changes I would suggest to use the state machine-pattern to control the transitions between states.