Player continues to moves up and down

I have noticed that my player object, the green sphere, will slowly fall up and down. When the space bar is pressed, the player is supposed to jump and the fall. But my player just vibrates up and down. If i turn off the gravity component of rigidbody then my player does not do the slow fall but still vibrates fast on space press.

public class PlayerMovement : MonoBehaviour
{
private Rigidbody rb;

private bool hasJumped;

public int playerHealth;

public Vector3 safeSpot;

public int speed;

public float velocity;
public float jumpforce = 20;
public float gravity = -9.18f;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody>();
    hasJumped = false;
}

private void FixedUpdate()
{
    float xMove = Input.GetAxisRaw("Horizontal");
    float zMove = Input.GetAxisRaw("Vertical");

    if (Input.GetKey(KeyCode.Space) && !hasJumped)
    {
        rb.AddForce(Vector2.up * jumpforce);
        hasJumped = true;
    }

    else if (hasJumped && rb.velocity.y !=0)
    {
        rb.AddForce(Vector2.down * jumpforce);
    }
    if(rb.velocity.y <= -9)
    {
        rb.velocity.y.Equals(0);
    }

    rb.velocity = new Vector3(xMove, 0, 0) * speed * Time.deltaTime;
}

public void damagePlayer(int damageType)
{
    if(playerHealth > 0)
    {
        playerHealth -= damageType;
        this.transform.position = safeSpot;
        this.transform.position = new Vector3(this.transform.position.x - 5, this.transform.position.y,
            this.transform.position.z);
    }
    else
    {
        //Game Over
    }
}

}

After applying the force with AddForce, you immediately set the velocity along the y axis to zero ( rb.velocity = new Vector3(xMove, 0, 0)…; ).
_
Try replacing the line in brackets above with the following:

rb.velocity = new Vector3(xMove, rb.velocity.y, 0) * speed * Time.deltaTime;

|

This will keep the player’s vertical speed at the required value.
_
EDIT:

I tested your code and realized that there is no need to multiply the vector by Time.deltaTime when assigning velocity (especially since you can only use Time.fixedDeltaTime in FixedUpdate). Also the code that replaces the gravity of the engine should be removed:

else if (hasJumped && rb.velocity.y !=0)
{
    rb.AddForce(Vector2.down * jumpforce);
}

So the FixedUpdate function should look something like this:

private void FixedUpdate()
{
    float xMove = Input.GetAxisRaw("Horizontal");
    float zMove = Input.GetAxisRaw("Vertical");
    if (Input.GetKey(KeyCode.Space) && !hasJumped)
    {
        rb.AddForce(Vector2.up * jumpforce);
        hasJumped = true;
    }
    else if (hasJumped && rb.velocity.y != 0)
    {

    }
    if (rb.velocity.y <= -9)
    {
        rb.velocity.y.Equals(0);
    }
    rb.velocity = new Vector3(xMove * speed, rb.velocity.y, 0);
}

P.S. Note that only one jump will be available in your game due to the hasJumped variable not resetting to false anywhere.

And I would put the value of the jump force around 500.