Collision and rebound

Hi everyone!

I am creating a similar pong game (yes, another pong) and I am unable to make the rebound. The main problem is to calculate the angle of the rebound and to make the ball move. Currently, the ball move, but to calculate the angle continuously is problem.

Ps: how post the code in better way?

This is my code until this moment:

// detect the ball collision
void OnCollisionEnter(Collision col)
{
float contactPoint = 0;

//if colide with the pads, execute action
if (col.gameObject.name == “Cube1” || col.gameObject.name == “Cube2”)
{
//return the contact point with paddle.
foreach (ContactPoint contact in col.contacts)
{
contactPoint = contact.point.x;
}

BallSpeed(contactPoint);
BallPosition();

//if colide with the walls, execute action
}
if (col.gameObject.name == “rightWall” || col.gameObject.name == “leftWall”)
{
BallPosition();
}

void Update () {
// transform.Translate(eixoX, 0, eixoZ);
//transform.Translate (new Vector3(radius * Mathf.Cos(angle), radius * Mathf.Sin(angle), 0));
transform.Translate(Vector2.left * speed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, angleY);
}

In order to post the code correctly - You can see an icon between the floppy disk and the photo film - This will open a few options, code post included.

As for the code…
Well, it’s all trigonometry.
Here are a few answers;

http://www.gamedev.net/topic/169021-best-pong-bounce-method/

Would Vector3.Reflect be the thing you’re looking for ? Unity - Scripting API: Vector3.Reflect You need to get the contact normal and then use it to get the new direction in which you ball should be moving.