Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nnatoli · Jan 11, 2017 at 08:57 AM · 2d collision

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JuliuszK · Jan 13, 2017 at 10:36 AM

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nnatoli · Jan 14, 2017 at 04:07 AM 0
Share

Thank you very much for your help! I finally got it working properly. You were absolutely correct, there was no need for me have any code that made changes to the velocity of the object on collision. It worked exactly as I wanted after I removed the code that made changes to the balls velocity on collision. Relying solely on the physics material (Friction 0, Bounciness 1), rigidbody2D, and colliders is definitely the right move. I also started using addForce to the rigidbody2D ins$$anonymous$$d of assigning it a velocity to begin it's movements.

Thanks again for the help!

avatar image JuliuszK nnatoli · Jan 14, 2017 at 08:53 AM 0
Share

No prob. Just a small clarification - setting the velocity at the start is O$$anonymous$$ - just not at every collision.

avatar image Avatar_k0 · Jul 15, 2019 at 08:52 AM 0
Share

"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."

I'm having that exactly problem. I tried changing the Balls RigidyBody2D.Collision Detection to Continuous, and its interpolate property to interpolate (and to extrapolate), I also tried increasing Position Interactions by 1000 times and decreasing Linear Correction and Baumgarte Scale to the $$anonymous$$imum, but the problem persists (even with all the said changes combined).

I'm out of ideas and variables to toggle, any help would be appreciated.

avatar image JuliuszK Avatar_k0 · Jul 15, 2019 at 06:47 PM 0
Share

Just to make sure - we're talking about a case, when you have let's say a row of bricks, the ball hits in between two bricks and it acts, as if it hit a corner of one of them, right?

Well, back in the days what I had tried was scaling up the collision boxes for the bricks in the middle of the row slightly, so they overlapped. This seemed to work a little bit better, but there were still some random incorrect bounces. Plus, after the adjacent bricks were gone, the remaining bricks were left with overscaled colliders, which wasn't good.

These days, I'd look into the CompositeCollider2D, which was introduced some time ago in Unity, which basically combines together a number of child colliders. This should create a single continues edge from a number of bricks and should solve these problems.

I haven't touched Unity in a while now and never used that feature, so can't tell exactly, but it looked promising.

avatar image Avatar_k0 JuliuszK · Jul 16, 2019 at 09:12 PM 0
Share

It worked!

I had to set the individual Colliders to be slightly bigger than their individual shapes, but with that and the Edge Radius of the parent set to zero, they beautifully merged into a single, solid shape!

Thanks for the help!!!

Show more comments
avatar image
0

Answer by millej23 · Jan 12, 2017 at 03:53 PM

Hello,

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:

https://www.youtube.com/watch?v=rq6c2B_socs

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by nnatoli · Jan 13, 2017 at 08:35 AM

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

BoxCollider2D not triggering 1 Answer

Ball wont destroy in collision even though that the ball are public GameObject 3 Answers

How can I avoid collisions between the same objects? 3 Answers

Lag Caused by Overlapping 2D Colliders 1 Answer

2D collision matrix does not work in unity 2020 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges