Hello everyone.

I’m making a 2D game with pads and balls, sort of like Pong, in Unity 4.6.1. The pads are going to appear in various rotations, and therefore the physics for bouncing the balls off the pads, have to be vector-based.

**EDIT: I’ve answered my own question (see below this post). Full code with many comments included.**

My wishes:

- Depending on where the ball hits the paddle, it should bounce off in an angle of between -90 and 90 degrees, where the middle of the pad would give 0 degrees, and the extremities on each side should give -90 or 90 respectively. (The numbers are just for reference.)
- The natural wall-bounce of the ball, should be taken into account, so the resulting bounce vector, will be a product of the normalized velocity vector of the ball, and the calculated and normalized directional vector from the normal wall/pad-bounce (see “Point 1”). (NOTE: The speed of the ball after the bounce is handled later!)

On the image below, the red arrow is the velocity vector of the ball. The effect of “Point 1” is the blue arrow, and the end-result implementing the functionality of both “Point 1” and “Point 2”, is the green arrow.

Additional information:

- The pads can change in size. I always have the following variables available: current width in units, standard width in units, and current scale as a float.
- The ball has a circle collider, which is not a trigger.
- The pads have a manually fitted polygon-collider, matching the bend on it. This is a trigger.

Using the code below, I almost have it going, but not quite. It’s not working consistently.

“Point 1” isn’t being computed correctly, mostly because I’m having trouble finding a calculation that’s precise. When I created my first Pong game, it was easy, because I could just see the difference in ball.y and pad.y and hold that against the width of the pad. I can’t for the life of me figure out how to do that using vectors.

“Point 2” is giving me trouble, because I can’t figure out how to calculate the normal vector for the pad. The calculations noted in this other answer and here say, that I need to get this normal correct to get the right bounce.

**This code is in “OnTriggerEnter2D(Collider2D col)” on the Pad:**

```
GameObject ball = col.gameObject;
BallScript ballScript = col.gameObject.GetComponent<BallScript>();
// We know the ball has hit the pad, so we raycast to see where it hits.
RaycastHit2D hit = Physics2D.Raycast(
new Vector2(ball.transform.position.x, ball.transform.position.y),
ball.rigidbody2D.velocity);
// Get the normal for the hit. I think this is also not going to work for my purposes,
// since my pad isn't square)
Vector2 n = hit.normal;
n.Normalize(); // this has to be normalized
// Get the velocity vector of the ball
Vector2 v = ball.rigidbody2D.velocity;
v.Normalize(); // I normalize v, because I want to add speed manually later
float dotOfvn = Vector2.Dot(v, n);
// Always reports -1f or 0.9998f, most likely because n is the same as v
// (or the exact opposite, I'm not sure)
Debug.Log("Dot: " + dotOfvn);
// Apply the function...
Vector2 R = -2 * dotOfvn * n + v;
// Without the following, the ball always returns in the same direction it came from.
// With the following, I almost have it all functioning, except when a ball comes
// flying as in image 2 (below).
R += (Vector2)((ballScript.transform.position - _transform.position) / currentPadScale).normalized;
R.Normalize();
ball.rigidbody2D.velocity = R * ballScript.speed;
```

Result of the current code is, that everything seems to almost work, but this image shows a situation that happens often, which proves it doesn’t. Red arrow is the velocity vector of the ball, and the blue is where the pad sends it upon being hit.

As the code-comments state, I’m not sure which parts I’m doing correctly, but I’m sure it is solvable. I would very much appreciate a helping hand.

Sincerely, Ultroman.