Search Unity

Collision Overlap issue

Discussion in '2D' started by RektByGrub, Nov 14, 2020.

  1. RektByGrub

    RektByGrub

    Joined:
    Aug 19, 2019
    Posts:
    4
    Hello All,

    Apologies I am quite new to Unity.

    I have been following along a 2D game making course, and they seem to have it all good, however I am facing a rather weird issue.

    It seems like my collisions are "soft".

    What I mean is when a collision happens, it looks like it bounces off like it would when hitting rubber, overlaps slightly and then bounces out.

    I notice the softness of the material more when a slow moving object collides with a fast moving one and the overlap is clear as day.

    Like below.

    upload_2020-11-14_18-26-10.png

    It is not game breaking but sure makes the game feel not well coded.

    Tried searching for similar things but nothing seems to come up.

    Any help would be much appreciated.

    Thanks,
    David
     
  2. Ruskul

    Ruskul

    Joined:
    Aug 7, 2014
    Posts:
    72
    I think, if you set Collision Detection to Continuous in the rigidbody component you will avoid this.
     
    devebasnet and RektByGrub like this.
  3. Ruskul

    Ruskul

    Joined:
    Aug 7, 2014
    Posts:
    72
    What is happening?

    Basically, when you have fast moving physics objects,the velocity gets added to the objects position during the physics update. It then checks for collisions and then resolves those collisions. In physics settings in project settings, there is a value to describe how fast an object can be resolved from colliding. You can play with that value, but it can make other aspects of physics seem unstable when you have heaps of objects all in contact.

    So, the faster the object moves, the larger the overlap may be when it collides with another object. Which, in turn means it takes more steps to resolve the collision. Checking Continuous Collision Detection means the object checks for collisions as it moves from its old position to its new one, preventing "deep overlaps" from happening in the first place.

    If you want to learn more about 2d physics in unity and how it works under the hood, it's helpful to know that unity uses box2d physics. You can google that and explore to your heart is content. Just keep in mind, there are some limitations in unity that aren't in box2d (pre collision solving, etc)....
     
    RektByGrub likes this.
  4. Ruskul

    Ruskul

    Joined:
    Aug 7, 2014
    Posts:
    72
    Also, if your objects are really small in unity units, that can change how physics behave. Box2d works best between certain sizes. basically between 0.1 and 10.

    If your game object is smaller or larger than these values, strange behaviors can happen.
     
    RektByGrub likes this.
  5. RektByGrub

    RektByGrub

    Joined:
    Aug 19, 2019
    Posts:
    4
    Hello,

    Thank you for your reply!

    When I set it to continous it does resolve the issue, but also makes it look like the object bounced off before even hitting.

    The box2d thing is really good to know thank you.

    I will resize my play area to be within 0.1 amd 10 unity units. I made it so its the same units as pixels (1440x1080), as i thought it would be easier to work with. Seems like this wasnt the best choice.

    I will tru your suggestions and see what happens.

    Once again thank you for taking the time to explain.

    Thank,
    David
     
  6. RektByGrub

    RektByGrub

    Joined:
    Aug 19, 2019
    Posts:
    4
    Hello Again,

    So with all of the below implemented:

    • Scaled down the play area from 1440x1080 to 8x6 (and made everything in the size between 0.1 and 10 unity units)
    • set the collision on the ball to Continuous instead of Discreet.

    This has solved my issues and looks much better now.

    Thank you for all your help Ruskul!

    Regards,
    David
     
  7. Ruskul

    Ruskul

    Joined:
    Aug 7, 2014
    Posts:
    72
    I'm glad I could help! I'm not the best at physics but I have done a lot of work with box2d, so if you come across any other problems, let me know.