mukul
mukul

Reputation: 181

Unity Brick Breaker: Ball hitting in-between two bricks

I am making a 2D Brick Breaker game in Unity.

I have an issue with the scenario when ball hits in between two bricks. I have BoxCollider2D attached to all bricks and a CircleCollider2D attached to the ball. When the ball hits between 2 adjacent bricks, it bounces back in the same direction as if it hit the edge of the brick. There is no edge in between, but two adjacent bricks form a continuous surface. So, the ball should bounce off of the surface (in other direction) instead of bouncing back.

enter image description here

Anyone knows of any solution to tackle this problem? I asked this in the Unity Physics forums but didn't get any answer, so checking if anywhere here might have had this issue.

Thanks, Mukul

Upvotes: 2

Views: 1755

Answers (4)

ConnonsWonderland
ConnonsWonderland

Reputation: 3

I am using a composite collider to handle specific collision events. I record all the objects that use the composite collider and place them into an array A. When a collision occurs, Record the collision coordinates and compare the distance from each element in array A. Then find the element in array A with the minimum distance, then process its collision event.

    private void OnCollisionEnter2D(Collision2D collision)
    {
       if (collision.gameObject.name == "EnemyPanel" || collision.gameObject.name == "PlayerPanel")
       {
           var grid = GridHolder.Instance.GetClosestGrid(collision.contacts[0].point);
           //Handle Collision
           //grid.transform.GetChild(0).GetComponent<HealthController>().DecreaseHealth(10f);
       }
   }

public GameObject GetClosestGrid(Vector2 point)
{
GameObject closestGrid = null;
float closestDistance = float.MaxValue;

foreach (GameObject grid in grids)
{
    float distance = Vector2.Distance(grid.transform.position, point);
    if (distance < closestDistance)
    {
        closestDistance = distance;
        closestGrid = grid;
    }
}

return closestGrid;
}

Upvotes: 0

NGS iVK
NGS iVK

Reputation: 1

I have also encountered the same problem.

My solution was to change the Fixed Timestep value: Edit -> Project Settings -> Time -> Fixed Timestep to the smallest possible value (0.0001)

As I watched the ball bounce back and forth, I realized that the collision detection was late, like a ball going through a brick. That makes the ball can hit 2 bricks or 2 collider edges at the same time.

Hope this helps.

enter image description here

Upvotes: 0

Andrew
Andrew

Reputation: 11389

Way 1:

Use one box collider for the wall, but not for every single brick. This will fix your issue + will optimize your project.

Way 2:

You need to build wall programmaly and colliders in this case must be without spaces between them. This must fix the issue.

Way 3:

Make your own hitting wall logic.

OnColliderEnter you need to get balls velocity. OnColliderEnd you need to set manually velocity.

Upvotes: 0

Anmol Mahatpurkar
Anmol Mahatpurkar

Reputation: 561

I am guessing this could be the problem:

When your ball is hitting the bricks with a strong force, There is a chance it might move one of the bricks by a very very slight distance, even if the mass of the brick is much heavier.

This movement might cause an uneven surface, which is why the ball bounces back in the same direction.

Try adding a Rigidbody Component on every brick (if you have not done it already), and set its isKinematic property to true.

Let me know if this solves it.

Upvotes: 0

Related Questions