How do I make the ball stop floating upward?

Update: I replaced the videos tweak with 3 tweaks and some if statements.

private void OnCollisionExit2D(Collision2D collision){
    Vector2 tweak1 = new Vector2(0.5f, 0f);
    Vector2 tweak2 = new Vector2(0f, 2f);
    Vector2 tweak3 = new Vector2(1.5f, 0f);
    if (hasStarted==true){
        AudioSource audio = GetComponent<AudioSource>();
        audio.Play();
    }
    if (hasStarted && this.GetComponent<Rigidbody2D>().velocity.x <= 2f && this.GetComponent<Rigidbody2D>().velocity.x > 0f) {
        this.GetComponent<Rigidbody2D>().velocity += tweak1;
    }
    if (hasStarted && this.GetComponent<Rigidbody2D>().velocity.x < 0f && this.GetComponent<Rigidbody2D>().velocity.x >= -2f){
        this.GetComponent<Rigidbody2D>().velocity -= tweak1;
    }
    if (hasStarted && this.GetComponent<Rigidbody2D>().velocity.x > 10f){
        this.GetComponent<Rigidbody2D>().velocity -= tweak1;
    }
    if (hasStarted && this.GetComponent<Rigidbody2D>().velocity.x < -10f){
        this.GetComponent<Rigidbody2D>().velocity += tweak1;
    }
    if (hasStarted && this.GetComponent<Rigidbody2D>().velocity.y <= 2f && this.GetComponent<Rigidbody2D>().velocity.y >= 0f){
        this.GetComponent<Rigidbody2D>().velocity += tweak2;
    }
    if (hasStarted && this.GetComponent<Rigidbody2D>().velocity.y <= 0f && this.GetComponent<Rigidbody2D>().velocity.y >= -2f){
        this.GetComponent<Rigidbody2D>().velocity -= tweak2;
    }
    if (hasStarted && this.GetComponent<Rigidbody2D>().velocity.y >= 10f){
        this.GetComponent<Rigidbody2D>().velocity -= tweak2;
    }
    if (hasStarted && this.GetComponent<Rigidbody2D>().velocity.y <= -10f){
        this.GetComponent<Rigidbody2D>().velocity += tweak1;
    }
    if(hasStarted && this.GetComponent<Rigidbody2D>().velocity.x == 0f){
        Debug.Log("X Plus " + this.GetComponent<Rigidbody2D>().velocity.x);
        this.GetComponent<Rigidbody2D>().velocity += tweak3;
        Debug.Log("X Plus " + this.GetComponent<Rigidbody2D>().velocity.x);
    }
}

Update: I got this working the way I wanted, but now realize if the ball is bouncing between the left and right walls, it sometimes can’t get high enough, so it keeps coming down and the player has to move the paddle back and forth with the ball until they hit with another part of the paddle. Is there a way to add y velocity to the ball if it hits the paddle?

I put the Random.Range at (-0.5f , 0.5f) for x and (-0.5f , 0f) for y, but when the ball goes side-to side for a while, it still floats up. The ball should only travel down, correct? How do I prevent this floating balloon ball?

1 Like

Is the gravity scale in your physics 2D settings a negative value in Y?

1 Like

Thank you Sebastian. I updated the title.

1 Like

Privacy & Terms