Skip to main content

Physics.ComputePenetration(Collider colliderA, Vector3 positionA, Quaternion rotationA, Collider colliderB, Vector3 positionB, Quaternion rotationB)

Compute the minimal translation required to separate the given colliders apart at specified poses.

Parameters:

NameTypeDescription
colliderAColliderThe first collider.
positionAVector3Position of the first collider.
rotationAQuaternionRotation of the first collider.
colliderBColliderThe second collider.
positionBVector3Position of the second collider.
rotationBQuaternionRotation of the second collider.

Returns:

typeDescription
booleanTrue, if the colliders overlap at the given poses.
Vector3Direction along which the translation required to separate the colliders apart is minimal.
numberThe distance along direction that is required to separate the colliders apart.

Usage

---@type Collider
local colliderA;

---@type Vector3
local positionA;

---@type Quaternion
local rotationA;

---@type Collider
local colliderB;

---@type Vector3
local positionB;

---@type Quaternion
local rotationB;


local val0, val1, val2 = Physics:ComputePenetration(colliderA, positionA, rotationA, colliderB, positionB, rotationB)

Extra Detail

Translating the first collider by direction * distance will separate the colliders apart if the function returned true. Otherwise, direction and distance are not defined.

One of the colliders has to be BoxCollider, SphereCollider CapsuleCollider or a convex MeshCollider. The other one can be any type.

Note that you aren't restricted to the position and rotation the colliders have at the moment of the call. Passing position or rotation that is different from the currently set one doesn't have an effect of physically moving any colliders thus has no side effects on the Scene.

Doesn't depend on any spatial structures to be updated first, so is not bound to be used only within FixedUpdate timeframe.