Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Objects passing through colliders

Discussion in 'Physics' started by mukulabdagiri, Aug 2, 2023.

  1. mukulabdagiri

    mukulabdagiri

    Joined:
    Jul 1, 2016
    Posts:
    21
    Hello, I am facing this issue where objects are passing through colliders about 10% of the time. My colliding objects are dynamic bodies. Some objects have Rigidbody2D, while some dont. For the ones that have Rigidbody, I have set collision detection to Continuous. Also tried playing with Physics2D settings (Default contact offset, Velocity iterations), but no luck. This is what was suggested in other threads. I also tried switching the colliders (BoxCollider2D, EdgeCollider2D) but objects still pass through at times.
    I dont know what else to do to get the collisions working 100%. Any particular Physics2D setting that controls this? Does Sleeping mode, Interpolation for Rigidbody matter in this case? Kindly suggest.
    Thanks, Mukul
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Are you changing the position/velocity/anything about the objects via script? If so, share the script.

    If collision is continues and your colliders aren't paper thin, it should collide with default physics.

    Sleep and interpolation has nothing to do with this most likely
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    If a rigidbody goes to sleep then a moving object that isn't a rigidbody will pass through it.

    Make all your moving objects a rigidbody.
     
  4. mukulabdagiri

    mukulabdagiri

    Joined:
    Jul 1, 2016
    Posts:
    21
    I tried adding RigidBody2D to all my colliding bodies but didnt make any difference with the collisions.
     
  5. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    it reduces performance drastically but you can change time fixed timestep go to edit> project settings >time >timestep reduce it about x10 plus if there is a trigger it may pass through it even it is continuous
     
    mukulabdagiri likes this.
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    If you edit transforms directly it will clip yeah
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    The Rigidbody2D is what writes to the Transform, not you. By writing directly to it, you're ignoring what the Rigidbody2D wants to do and stomping over the position with your own values. Use the Rigidbody2D API to effect movement in all cases without exception.

    Sleeping mode nor Interpolation affect collision detection. Bypassing the physics system affects collision detection.
     
  8. mukulabdagiri

    mukulabdagiri

    Joined:
    Jul 1, 2016
    Posts:
    21
    Thanks a lot. This seems to have solved the collision bypassing. Also thanking everyone else for your help.
     
  9. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    It will also help to keep your home warm through the winter..
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    That isn't a fix and nobody should be increasing the simulation frequency by x10. That's a huge extra performance hog you don't need and is likely to cause you other problems. Let's stop advising that please.

    The problem is that you're directly modifying the Transform. Stop doing that.
     
  11. mukulabdagiri

    mukulabdagiri

    Joined:
    Jul 1, 2016
    Posts:
    21
    I tried adding Rigidbody2D to all the colliding objects and then moved them by changing the velocity or adding force instead of modifying the transforms, but that did not solve the issue. The objects were still bypassing. Do you think i am missing anything?
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Well how fast are they moving? Did you try continuous if they're moving fast (without modifying the Transform)? That never fails. Discrete by its nature allows things to step over each other but is cheap to compute and most of the time isn't required. If you're firing things against a single edge (that has no inside) then it can easily step over the edge because it has no "inside".

    If changing the simulation step to x10 the frequency (kind of crazy!) solves it, it means you're using discrete collision detection with fast moving and/or slim objects against each other or yo're still modifying the Transform somewhere.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Well the Continuous col-det is absolutely rock solid so you're doing something wrong. I'm not sure what else to say.