How to change angle after collision, Tips?

So, here’s the deal. I’m reasonably new with game creating and scripting. And I thought about making a brick breaker game for fun. I made all the power ups, bricks and such but I’m having problem with the pad. I know why it doesn’t work but i have no idea how to create such a script or where to start. I want the angle of the ball to change after the collision depending on the spot it hits on the paddle. F.e. if it hits the middle it goes straight up, and if it hits the middle of the left side it leaves in approx 45dgr angle. Any ideas? And I’m using JavaScript.

I’ve been struggling with this for a few days now, searching the internet but with no success. Help appreciated :)!

I’m not sure how much you already know about Unity. You will need to use Unity’s physics system. To do that you will need to add a Rigidbody and a Collider on both the paddle and the ball. You will then need a script on the paddle that uses the OnCollisionEnter function (this is run whenever a collider hits the paddle’s collider, in this case the ball’s collider we hope). OnCollisionEnter will pass a parameter of type Collision which will give you the contact point(s) of the collision. From that collision point, you can calculate the horizontal distance from the collision point to the center of the paddle.

Let’s say your paddle is 20 meters wide, then if the ball hits on the left side of the paddle the horizontal distance may be -5. Then you want to do some math that will convert the range of -10 to +10 meters to the range of -45 to +45 degrees.

You can do that conversion by dividing your horizontal distance by half the width of your paddle. Then you multiply that by your max degrees which is 45. In this example we have:

-5/10 = -0.5
-0.5 * 45 = -22.5

just use physics…
after collision just disble the fixed angle property of the rigidbody component…
put this in OnCollisionEnter2D…

rigidbody2d.fixedAngle=false;