Search Unity

  1. Unity 2018.3 is now released.
    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. Our response to Improbable’s blog post (and why you can keep working on your SpatialOS game). Read more here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

ComputePenetration, what am I doing wrong?

Discussion in 'Physics' started by Survivalist-Games, Jul 27, 2018.

  1. Survivalist-Games


    Nov 24, 2014
    Hi all.

    I'm having an issue, that PhysicsCompute penetration always seems to return false, and I can't see why. I'm getting colliders with an overlap test and immediately checking them. Here's the code:

    Code (CSharp):
    2. Vector3 GetDepenetrationVector()
    3. {
    4.     Collider[] hits = new Collider[8];
    5.     Vector3 position = transform.position +;
    6.     Vector3 offset = new Vector3(0f, (characterController.height * 0.5f) - characterController.radius, 0f);
    7.     int contacts = Physics.OverlapCapsuleNonAlloc(
    8.         position - offset, position + offset, characterController.radius, hits
    9.     );
    10.     if (contacts == 0)
    11.         return;
    13.     // Check hit
    14.     Vector3 result =;
    15.     for (int i = 0; i < contacts; ++i)
    16.     {
    17.         // Calculate overlap
    18.         Collider c = hits[i];
    19.         Vector3 overlap;
    20.         float distance;
    21.         bool overlapped = Physics.ComputePenetration(
    22.             characterController, transform.position, Quaternion.identity,
    23.             c, c.transform.position, c.transform.rotation,
    24.             out overlap, out distance
    25.         );
    27.         Debug.Log("Checking against " + + ", overlapped: " + overlapped);
    29.         if (overlapped)
    30.             result += overlap * distance;
    31.     }
    33.     // Return result
    34.     return result;
    35. }
    I am using a CharacterController as the first collider (on the object this behaviour is attached to).

    I've tried using position, position + center, etc. I've also tried casting the other collider to a box in a known test situation and using position + center for that as well. I've tried transform.rotation for the character collider, but assume that's not necessary.

    Also, I should point out that in the scenario I use it for, the other colliders are always a simple collider (capsule, box or sphere) and always has a rigidbody.

    No matter what I do, the debug log always says that ComputePenetration returned false.

    Any help would be appreciated.
  2. hippocoder


    Digital Ape Moderator

    Apr 11, 2010
    I would suggest doing a bug report as it's not certain there are no bugs with this.
  3. PandaArcade


    Jan 2, 2017
    @Survivalist-Games did you ever submit a bug report for this? I'm having similar problems with ComputePenetration intermittently incorrectly returning false :(
  4. Survivalist-Games


    Nov 24, 2014
    I didn't actually. I got distracted with other tasks.

    I can do that before the end of the week, unless you have a test case you can set do that with yourself?

    Edit: Also I'm not using anything near the latest version so it might be better doing a bug report with that if it's still a problem. I'm developing an asset, so I wanted to find a workaround that works in the version I'm using as that's the minimum version I plan to support
  5. PandaArcade


    Jan 2, 2017