I wanted to improve the ball script for making more arkanoid style: depending on the contact point when ball hits the paddle, change it’s direction as the next image:

In the ball script first i made a float variable called speed magniture, from Ben video the starting magnitude was abount 10 (sqrt(10^2 + 2^2)), so I kept it.

private float speedMagnitude = 10f;

Then, when mouse is pressed I’ve randomized the x between -1 and 1, normalized the vector( so it’s magnitude it’s 1) and multiplied by speedMagnitude:

rb2d.velocity = new Vector2(Random.Range(-1f, 1f),1).normalized * speedMagnitude;

Debug.Log(rb2d.velocity.magnitude);

I’ve assigned a “Paddle” tag to the paddle, and used in OnCollisionEnter2D as following:

if(collision.gameObject.CompareTag(“Paddle”))

{

//distance from paddle’s center (negative if less than 0, positive if greater than 0, of course 0 if 0)

float ballDistance = transform.position.x - collision.transform.position.x;

//percentage between the ball hitting poi and the half leght of the paddle (has values between -1 and 1)

float ratioDistance = (ballDistance / (paddleLenght / 2));

//new velocity depending on the hit point on the paddle

rb2d.velocity = new Vector2(ratioDistance, 1).normalized * speedMagnitude;

print(rb2d.velocity.magnitude);

}

the velocity tweak when hitting walls can be placed on the else statement if you want to vary the direction.

Notes:

- If we want to make the game more engaging with increasing the velocity (ex. when hitting a n number of brick, when collide, a malus picku, ecc…), the only variable is speedMagnitude.
- a few link of the math (vector) operation used: scalar multiplication, normalized vector

Try it if you want and send me feedback, tell me if I made any mistake!