Problem: Jump with input system & rigidbody (2D)

Hi, I’m learning how to use the package Input System, I have this code.

The problem is, when I press one of the Jump bindings (I have the bindings with an action map), usually It doesn’t work, with rb.AddForce, if I do it with transform.Translate() the player jumps without problems. The gravity of the project is in -9.81, the gravity scale of my player is in 50, and his mass is in 1. I’ll add a video with the problem, every time the consoles says "Jump" one of the Jump bindings was pressed.

I wrote this on reddit, I put the video there, here’s the link of the post Reddit - Dive into anything

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class MovimientoJugador : MonoBehaviour
{
    #region variables

    public PlayerInput playerInput;
    private Rigidbody2D rb;
    private PlayerControls myinput;
    public float velocidad = 10f;
    public float salto = 10f;
    private Vector2 movimiento;
    private float movimientox;

    #endregion

    void Start()
    {
        rb= GetComponent<Rigidbody2D>();
        playerInput= GetComponent<PlayerInput>();
        myinput = new PlayerControls();
        myinput.Exploracion.Enable();
    }


    void Update()
    {
        ProcesarMove();
        ProcesarJump();
    }

    private void ProcesarMove()
    {
        #region Valores de movimiento
        movimiento = myinput.Exploracion.Move.ReadValue<Vector2>();
        movimientox = movimiento.x;
        #endregion

        #region Movimiento Horizontal
        rb.velocity = new Vector2(movimientox * velocidad, 0f);

        #endregion

     }

    private void ProcesarJump()
    {
        #region Salto

        if (myinput.Exploracion.Jump.WasPressedThisFrame())
        {
            Debug.Log("Jump");
            rb.AddForce(Vector2.up + new Vector2(0f, salto), ForceMode2D.Impulse);
        }
        #endregion
    }
}

If you didn’t understand which is the problem who I have let me know and I´ll try explain it better, it’s a bit difficult to me because my english isn’t very good.

your code makes perfect sense. except the

rb.AddForce(Vector2.up + new Vector2(0f, salto), ForceMode2D.Impulse);

i would try:

rb.AddForce(Vector2.up * salto, ForceMode2D.Impulse);

also instead of:

 if (myinput.Exploracion.Jump.WasPressedThisFrame())

i would add a isGrounded variable so:

public PlayerInput playerInput;
private Rigidbody2D rb;
private PlayerControls myinput;
public float velocidad = 10f;
public float salto = 10f;
private Vector2 movimiento;
private float movimientox;
*private bool isGrounded; // remove the Stars (*) and not (/* or */)
public float RayDistance;* /* use this to define how far the ray goes down. the longer the ray is the longer isGrounded will be true after leaving the ground which may help as the player will keep adding the jump force while the ray is hitting the ground not while the player is.*/

 private void ProcesarJump()
{
    #region Salto
    isGrounded = Physics2D.Raycast(transform.position, Vector2.down, RayDistance);
    if (myinput.Exploracion.Jump.readValue<float>() && isGrounded)
    {
        Debug.Log("Jump");
        rb.AddForce(Vector2.up * salto, ForceMode2D.Impulse);
    }
    #endregion
}

i changed the way the jump force is calculated as well as adding a variable to store whether the player is grounded are not instead of just jumping very time the button is pressed. however you will need to change the inputAction type from button to PassThrough with a value type of button. i am not sure if this will fix your problem. but at least adding the RayCast setup alows you to increase the length of the ray via the RayDistance variable, and set the Ray’s distance to be much longer than neccesary so that your character will keep jumping continuously while you hold the button. so you can tell whether the character’s Rigidbody is not reacting to the force of jumping or if there is some other forces being applied to the rigidbody. some things i would look for are: the gravity being too high. from the reddit video it appears as though the gravity on your charcter object is super high which could cause the jump force to be un-noticeable because the gravity pulls the charcter back down instantly and the few times it does jump is because the Update() method Came out of sync with the FixedUpdate() which the RigidBody2D runs on. so the first thing i would do is set the gravity to “1 1” then set jump Force (salto) to be very low < 1 and then increase the RayDistance variable until you get the “jump” message in the console and if your player flies off into the distance change the

rb.AddForce(Vector2.up * salto, ForceMode2D.Impulse);

to

rb.AddForce(Vector2.up * salto, ForceMode2D.VelocityChange);

and if you really want the gravity to be super high. still test for whether the gravity is causing the problem and if it is you can set the

rb.AddForce(Vector2.up * salto, ForceMode2D.VelocityChange);

back to

rb.AddForce(Vector2.up * salto, ForceMode2D.Impulse);

and set the jump force to be super-duper high to counterract the gravity. and remember if you aren’t getting the “jump” message in the console make sure that the RayDistance variable is set high enough.
i hope this helps!

The problem in the code was:
in ProcesarMove() I wrote

rb.velocity = new Vector2(movimientox * velocidad, 0f);

Then, when that function was performed, the y force was setted to 0 again