I have private string _SPACEKEY = "spacekey"
and string _buttonPressed
. In Update() I catch user input like this
if (Input.GetKeyDown(KeyCode.Space))
{
_buttonPressed = _SPACEKEY;
}
And in FixedUpdate() I try to make player jump like this
if (_buttonPressed == _SPACEKEY)
{
_playerRigidBody.AddForce(new Vector2(_playerRigidBody.velocity.x, _playerSpeed * 1.5f), ForceMode2D.Impulse);
}
else //need this to stop player movement when A or D not pressed
{
_playerRigidBody.velocity = new Vector2(0, _playerRigidBody.velocity.y);
}
But that doesn’t work the way it supposed to. Player doesn’t jump each time I press Space. I tried to Log “if” statement in FixedUpdate and it works about 1 time out of ten when I spam Space button.
I tried to put bool in GetKeyDown (like jump = true
) but it didn’t work neither.
Works perfectly if I place Jump logic in Update() but I don’t want to do that. What’s the way to solve this?