# Infinite ball bouncing problem (Unity)

I am trying to develop a basic Breakout game using this tutorial using Unity 5. But the ball starts to bouncing infinitely very easily. I cannot understand why this happenes.

In my physics knowledge starting an infinite bounce is very hard, and even impossible. To return the ball to same position after bouncing we must send the ball with zero degrees with the vertical axis. To send the ball with zero degrees with vertical axis we must receive the ball with zero degrees with the vertical axis. To receive the ball with zero degrees with the vertical axis we must send the ball with zero degrees, and so on. We can send a ball with zero degrees only in initial ball throw. In all the other cases there must be an angle which is not equal to zero. Am I wrong?

I mean the ball does not change its rotation. Ball moves between two plates which are parallel to each other infinitely (for example between paddle and upper wall)

I added physics material to ball, paddle and the walls. This material has following settings: Dynamic friction: 0 Static friction: 0 Bounceness: 1

Settings of the ball: Mass: 0.5 Drag: 0 Angular Drag: 0 Is Kinematic: true (I changed to false just before firing) Use Gravity: false

Settings of the paddle: Mass: 0.5 Drag: 0 Angular Drag: 0 Is Kinematic: true Use Gravity: false

• What do you mean by infinitely? does that mean that it starts to bounce higher and higher, or that it just keeps bouncing without reducing its bounce?? The video you gave is an hour long and nobody will watch it to find out what type of algorithm is being used. You will need to give a clear explanation as to the problem to get help. – Blindman67 Jul 13 '16 at 7:46
• What physics parameters are you using? – Droppy Jul 13 '16 at 7:57
• @Blindman67 I mean the ball does not change its rotation. Ball moves between two plates which are parallel to each other infinitely (for example between paddle and upper wall) – user1067742 Jul 13 '16 at 8:15
• Have you tried adding some dynamic friction to both the ball and the bat. Then when the bat is moving that should impart some sideways movement into the ball. – Blindman67 Jul 13 '16 at 8:41
• In brickbreaker games, even if the paddle looks flat, it does not physically react as such: you can consider that it has the surface of an arc of a circle, like a half of a hockey puck. When the ball hits the center, it is returned as if the paddle is flat, but as you go toward the edge of the paddle, an angle is added so the ball can change direction. – Vaillancourt Jul 13 '16 at 11:51

I think you mean that the trajectory (the direction it is traveling) does not change, such that the ball bounces perfectly up and down between the two same points on the parallel plates. You are correct that this is extremely difficult and nearly impossible in real world conditions, because there are so many factors that can alter the ball's path.

However, you have created two perfectly parallel planes in the Unity physics model, and I assume that you have not added any external forces such as wind. If the ball is launched vertically, why would there be any change to its angle on the first bounce or any subsequent bounces? When Unity's perfect sphere collides with the perfect plane, the ball will bounce vertically in the opposite direction. This will continue indefinitely unless some additional force comes into play.

As Alexandre mentioned in the comments, you could add some variation in the bounces by giving the paddle a curved surface. This adds a game mechanic where bounces are easier to control closer to the center of the paddle.

You could also add friction to the physics materials, as Blindman suggested. Your current implementation has zero friction, so a moving paddle will have no effect on the bounces. If you set a nonzero friction, then players can alter the bounce direction by moving the paddle right when it hits the ball.

However, both of these solutions will still allow indefinite vertical bouncing if the player simply leaves the paddle in its original position. If you want to completely prevent that situation, you could add some randomization to the bounces. You could do this by applying a slight random horizontal force to the ball on each bounce, or by slightly changing the angle of the planes between bounces. These changes would not have to be very large to prevent an indefinite vertical bounce situation.

The rotation of the ball is a separate issue, but adding friction will also cause the rotation to change with each bounce.

I know this has been posted quite some time ago but for the sake of others who may stumble upon this I will try to answer this as I experienced the same problem a few days ago. You can either solve it by  randomizing the velocity after each collision to ensure that you will always get a slightly different path for your ball, or  you may change the ball path by adding force to either x or y velocity once it is nearing zero(meaning that the ball is starting to stagnate at the same position). I will show both answers below. I guess it is up to you which one you prefer. Note that that these methods must be implemented on the ball game object script.

1. Random velocity tweak after each collision
[SerializeField] float randomFactor = 0.3f;

//ball object collision
private void OnCollisionEnter2D(Collision2D collision){
RandomVelocityTweak();
}

private void RandomVelocityTweak(){
//create random velocity tweak from 0 - 0.3f
Vector2 velocityTweak = new Vector2(Random.Range(0f, randomFactor), Random.Range(0f, randomFactor));
myRigidBody2D.velocity += velocityTweak;
}

1. Adding force to either x or y velocity
private void OnCollisionEnter2D(Collision2D collision){
SuddenXVelcoityTweak();
//tweak y velocity
SuddenYVelcoityTweak();
}

//Left and right (x) stagnation of ball: use y velocity tweak to change direction to either up or down
private void SuddenYVelcoityTweak() {
float min_y_velocity = 1f;
//if y velocity is lower then min y velocity and y velocity is positive (ball direction: up)
if ((myRigidBody2D.velocity.y  -min_y_velocity) &&
(Mathf.Sign(myRigidBody2D.velocity.y) == -1))
{
//push ball down
}
}

//Up and down (y) stagnation of ball: use x velocity tweak to change direction to either left or right
private void SuddenXVelcoityTweak()
{
float min_x_velocity = 1f;
//if x velocity is lower then min x velocity and x velocity is positive (ball direction: right)
if ((myRigidBody2D.velocity.x  -min_x_velocity) &&
(Mathf.Sign(myRigidBody2D.velocity.x) == -1))
{
//push ball left
}
}


Then if you want you may add after how many bounces the ball should change direction

int bounceCount = 0;

private void OnCollisionEnter2D(Collision2D collision)
{
//increment bounce count
bounceCount++;

if (bounceCount % 4 == 0) {