How do I properly handle a collision between a ball and a rectangle in 2D?

I am creating a brick breaker style game and I am having an issue with how my ball reacts when hitting the side of a brick. If I control the ball by reversing it’s velocity upon collision, then the collision only looks natural when colliding with the top or bottom of the brick. When the ball hits the side of the brick it is immediately sent in the opposite direction and it looks very unnatural. I have tried using ray casting to identify which side of the brick has been hit and reversing the velocity accordingly. However, that solution wasn’t consistent enough and when a full scene of bricks was introduced it failed miserably. What is the best way to handle a situation like this?


Have you tried using Rigidbody on the objects and the use of colliders? I havent done too much 2D but it sounds like with ball physics Rigidbody might be what you are looking for?

Here is the Unity 2D rigidbody tutorial:

The ball has a Rigidbody2D and a circle collider and the brick sprite has a box collider. The problem is my solution for handling that collision doesn’t look as natural as I want. I can detect the collision and delete brick, however, without any additional code the ball will just continue to move forward. Ideally, I would like the ball to bounce off in the proper direction. I have tried attaching physics materials with 0 friction and 1 bounciness but the ball still goes through the sprite after deleting it. I have also added code that reverses the y velocity upon collision with the brick, which works great unless it hits the side of a brick then it doesn’t look right.

I guess what I am asking is if there is a way to detect whether you hit the horizontal or vertical sides of a rectangle without ray casting or is there a way to give the ball the ability to naturally bounce off of objects while maintaining the appropriate velocity by configuring rigidbody’s and such? It might be important to note that I currently allowing the ball to move by assigning values to the rigidbody2D velocity. I’ll look into using force instead and see if that makes any difference.

Thanks for the help btw!

It’s definitely possible to achieve it just by adding Rigidbody2D and Collider2D components - no need to code the collisions manually - that’s what the physics engine is for. I’m shortly after doing this type of a game in one of the courses on Udemy. Here’s what I did:

Brick - BoxCollider2D (Is Trigger OFF)

Ball - CircleCollider2D (Is Trigger OFF), Rigidbody2D (Kinematic OFF) with Physics Material (Friction 0, Bounciness 1)

If your ball passes through the bricks then maybe you have Is Trigger option ON - this is used only for detecting if something goes into the bounds of the collider. It needs to be OFF on both colliders for them to bounce off each other.
You may also want to tweak the Gravity Scale either globally or for the ball only, so it does not fall down with the gravity.

Having said the above, you may notice unpredictable bounces when the ball hits the corner of the brick, but that’s rare and may not be a problem to you.