gravity disables when i die

I’m making an endless runner game and I made a script that makes the ground move faster as time goes, so to make it so the player will move as fast as the ground does i got the speed of the ground from the ground script

        rb = this.GetComponent<Rigidbody2D>();
        playerSpeed = GameObject.FindObjectOfType<MovingFloor>().groundVelocity;

        if(isTouchingObstacle)
        {
            rb.velocity = new Vector2(-playerSpeed, 0);
        }

i found that if i set the second float in rb.velocity it will change the gravity, but it will instantly push me down instead of keeping the momentum that I had before i died if that makes any sense

It’s because you’re setting the Y velocity to 0. To keep the same momentum set the Y velocity to whatever it previously was.

rb.velocity = new Vector2(-playerSpeed, rb.velocity.y);