Rigidboyd doent jump

title says it all

script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerMovement : MonoBehaviour
{
    Rigidbody rigi;
    public float speed = 10f;
    public float jumpSpeed = 10f;
    public bool grounded;
    void Start()
    {
        rigi = this.GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        float X = Input.GetAxis("Horizontal");
        rigi.velocity = new Vector3(X * speed, 0, 0);
        if (grounded && Input.GetKeyDown(KeyCode.Space))
        {
            rigi.AddForce(new Vector3(0, jumpSpeed,0), ForceMode.Impulse); //this doesnt work i think
            Debug.Log("jump");  //this pops up so that if statement works
        }
    }
    public void OnCollisionEnter(Collision collision)
    {
        grounded = true;
    }

    public void OnCollisionExit(Collision collision)
    {
        grounded = false;
    }
}

note: this script doent give any errors, it just don’t work

You jump one frame, then immediately set the y velocity to 0 the next frame.

Try something like this so you don’t mess with the other axes:

Vector3 currentVelocity = rigi.velocity;

currentVelocity.x = X * speed;

rigi.velocity = currentVelocity;

Or instead of doing this:

rigi.velocity = new Vector3(X * speed, 0, 0);

you want to use your rigidbody actual velocity, so you don’t mess if your jump or any other future changes that you may do.

rigi.velocity = new Vector3(X * speed, rigi.velocity.y, rigi.velocity.z);