This is my code for Jump Action using the new input system:
/// JUMP action callback
public void Jump(InputAction.CallbackContext context)
{
if (context.performed)
{
//hold down button: full height JUMP
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, jumpPower);
}
else if (context.canceled)
{
//light pressure: half height JUMP
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, jumpPower * 0.5f);
}
}
It works fine, but to avoid the player floating around, I also added a function that allows him to jump only if he is touching the ground. This is the function:
private bool isGrounded()
{
if (Physics2D.OverlapBox(groundCheckPos.position, groundCheckSize, 0, GroundLayer))
{
return true;
}
else
{
return false;
}
}
Hence, I modified the jump function as follows:
/// JUMP action callback
public void Jump(InputAction.CallbackContext context)
{
if (isGrounded())
{
if (context.performed)
{
//hold down button: full height
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, jumpPower);
}
else if (context.canceled)
{
//light pressure: half height
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, jumpPower * 0.5f);
}
}
}
The issue is that now the jump is always FULL height, the light pressure is not working anymore. By debugging the code using some print statements, I observed that by performing the same action (i.e., slow button pressure for half jump) almost always Unity executes the *context.performed *action. Only rarely, it executes the half jump.
I only inserted a condition on a bool variable. I cannot figure out why it is not working correctly any more. Am I missing something?
1