# 2d bouncing ball script.

Hi everybody, i’m trying to make a 2d bouncing ball. I found in the site of unity a tuto and in some forums some scripts for 3d ball; In these scripts they use some components i don’t have in a 3d object. Someone knows how i can do my bouncing ball. I want my ball changes randomly direction when it touches a wall.
I you have a suggestion, i take.

To make a 2d ball bounce, put a rigidbody2d and a circle collider 2d on the 2d ball. Then add a 2d physics material on the ball and set its bounciness to 1. When it falls on your 2d ground that has a collider on it, it should bounce.

To have it bounce in a random direction, look up OnCollisionEnter2D. By using that, you can give the ball a random velocity by using AddForce and some random number with the Random class.

1 Like

Thanks a lot. But what’s the name 2d physics material i have to add

It’s ok. thanks

There is no name

It’s work fine and here a tuto to

i found the solution in this video:

1 Like

I sorry for asking but how to you make its volicity stay the same when it bounces

probably don’t still need this but you can set max velocity like this

public Rigidbody2D rb;
public float maxSpeed = 10.0f;

void Update()
{
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxSpeed);
}
}

I believe the equation is

new velocity = 2 * (V dot N) * N - V

V = velocity, N = normal of the surface you bounce off
This will work in 2d or 3d