# Breakout/Pong ball stuck to wall problem

Hi people, in my current project I have a ball, paddle and 3 walls, left, top and right. I have a problem where that if I get it just right (and I pretty much can whenever I want) I can make the ball slide up and down the right/left wall. Now, this isnâ€™t really the desired effect that I was looking for in a game where the ball is meant to constantly bounce around

I have tried a couple of things; firstly, I tried adding extra velocity to the ball when it entered the walls collider/trigger. This worked in most situations apart from when the ball seems to hit the wall at a vertical straight line. Its almost as if the ball is somewhat stuck to the wall by static or a magnet. I then tried putting a couple of spheres in each corner to see how that would make the ball react as it was sliding up and down the wall. This gives some very random results!

What I am hoping for is a solution or any help, in code or with something I can add to my ball/walls in order to stop this sliding motion.

This is a video of what I mean:

Cas

It looks like this is a problem you could run into at any point on the playfield, not just the sides - you can get the ball bouncing perfectly up/down in the center of the screen if you put the paddle in just the right place, no?

How are you calculating the ballâ€™s bounce vector when it hits the paddle?

Yes, you can make it bounce up and down from the paddle if you get it right. Thatâ€™s not too much of a problem as there will be objects that fall which you have to dodge; thus making you move the paddle.

The code for making the ball a little more random off the paddle is this:

``````void OnCollisionExit (Collision collisionInfo)
{
Rigidbody rigid = collisionInfo.rigidbody;
float xDistance = rigid.position.x - transform.position.x;
rigid.velocity = new Vector3(rigid.velocity.x + xDistance - 0.2f, rigid.velocity.y, rigid.velocity.z + 0.2f);
}
``````

Excuse the numbers and the way it looks, I was messing about with it and havenâ€™t bothered to change it back yet. Depending on where the ball hits the paddle, the direction of the ball will head towards that direction. I could just ramp up the numbers to properly stop what you said but I have found that if I go above 32 velocity, the ball goes through the paddle.

You could simply add a small random value to the x component there:

``````rigid.velocity = new Vector3(rigid.velocity.x + xDistance - 0.2f + Random.Range(-0.1f, 0.1f), ...
``````

I see where youâ€™re coming from but unless I add larger number within the random range (like -2,2 or higher) it will not really affect the ball at all. So yeah, I can get it to come off the wall with higher numbers but then I get odd bounces off the paddle. For instance, I just did it with -1,1 and when the ball hit the right side of the paddle, the ball tried to go left.

OK, so ensure that the random value has the same sign as the position delta:

``````rigid.velocity = new Vector3(rigid.velocity.x + xDistance + Mathf.Sign(xDistance) * Random.Range(0.1f, 0.2f), ...
``````

That seems to work really well, thanks very much! =D

Where in the code do i pust it for instance where would i place it in here exaclty?

using UnityEngine;
using System.Collections;

public class BallMovement : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
}

If youâ€™re using velocity, why not just make rigidbody.velocity.x = rigidbody.velocity.x * -1; when you hit it?