gravity disables once i get eliminated

i’m making an endless runner game and the floor’s velocity slightly increases as time goes, i tried to make it so when the player gets eliminated he will move as fast as the floor does because he will no longer be sprinting but when i do that it disables the gravity after the player gets eliminated

in the following script I made it so when you touch an obstacle you will move as fast as the ground does by getting the float from the ground script into the player script, i did try changing the second float in rb.velocity but it seems to just pull me to the ground without keeping my momentum

        rb = this.GetComponent<Rigidbody2D>();
        playerSpeed = GameObject.FindObjectOfType<MovingFloor>().groundVelocity;
        if(isTouchingObstacle)
        {
            rb.velocity = new Vector2(-playerSpeed, 0);
        }

hope that made sense

It’s not disabling the gravity. Line 5 above is zeroing the velocity, so any gravity acting on it gets removed. The gravity is still there acting, but you’re saying that as long as the player is touching an obstacle, do not fall.

Perhaps you only want to set the X velocity and leave the Y velocity alone? It would look like:

Vector3 vel = rb.velocity;
vel.x = -playerspeed;
rb.velocity = vel;[/code
1 Like