I am trying to implement some basic 2D movement using the input system. I have created an Action Map called Player with 2 actions, Jump and Move. After that I have generated a C# class for this Input Actions.
The horizontal movement works fine but when I try to jump using space the character does not move until I press space multiple times, after that the character launches into space disregarding the float value I have specified.
How can I make the character react to the first space press?
Here itβs the code for for the player script:
public class Input : MonoBehaviour
{
private Rigidbody2D rb;
private PlayerInputActions playerInputActions;
[SerializeField]
private float moveSpeed = 4f;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
playerInputActions = new PlayerInputActions();
playerInputActions.Player.Enable();
}
private void FixedUpdate()
{
Vector2 inputVector = playerInputActions.Player.Movement.ReadValue<Vector2>();
rb.velocity = new Vector2(inputVector.x, rb.velocity.y) * moveSpeed;
if (playerInputActions.Player.Jump.triggered)
{
Jump();
}
}
public void Jump()
{
Debug.Log("Trying to jump");
rb.velocity = Vector2.up * 3f;
}
}