Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Overlap after depenetration?

Discussion in 'Physics' started by Epiplon, Sep 7, 2018.

  1. Epiplon

    Epiplon

    Joined:
    Jun 17, 2013
    Posts:
    21
    So, I was running a few tests with
    Physics.ComputePenetration
    and
    Overlap
    functions, mainly for making custom collision detection and noticed something.

    Why does Overlap functions return True when colliders are really close but not touching?

    My basic setup was to create a CapsuleCollider and a BoxCollider. The capsule would move against the box and fix the collision. Then, right after that, the object with the box collider would run their Update and check for anything that might be overlapping. This was done setting the order of the scripts in the Script Execution Order settings. Turns out the Capsule was overlapping anyway.

    This is the depenetration Update:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         transform.position += transform.forward * Time.deltaTime * 2f;
    4.  
    5.         var n = PhysicsExtensions.OverlapCapsuleNonAlloc(capsule, colliders, layerMask);
    6.         for (int i = 0; i < n; i++)
    7.         {
    8.             var collider = colliders[i];
    9.  
    10.             if (Physics.ComputePenetration(capsule, transform.position, transform.rotation,
    11.                 collider, collider.transform.position, collider.transform.rotation,
    12.                 out penetrationDir, out penetrationDist))
    13.             {
    14.                 transform.position += penetrationDir * penetrationDist;
    15.             }
    16.         }
    17.     }
    And this is one from the box (named Wall in the demo):
    Code (CSharp):
    1. void Update()
    2.     {
    3.         c_count = PhysicsExtensions.OverlapBoxNonAlloc(boxCollider, overlapColliders, playerLayer,
    4.             QueryTriggerInteraction.Ignore);
    5.     }
    I set them up with different layers in order to avoid any confusion.

    If anyone wants to test it, here is the link:
    https://1drv.ms/u/s!AhuKiONNB5NIh8RJVYAv9FHV14xmTg
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,171
    Epiplon likes this.
  3. Epiplon

    Epiplon

    Joined:
    Jun 17, 2013
    Posts:
    21
    Oh, that's right. It makes sense.