Search Unity

Avoid overlap of BoxColliders

Discussion in 'Physics' started by MJost, May 7, 2019.

  1. MJost

    MJost

    Joined:
    Jul 28, 2015
    Posts:
    11
    Hi everyone,

    I want to achieve that BoxColliders don't overlap at any given time. Is this possible at all?
    I am setting the new position using rigidBody.MovePosition() on a kinematic BoxCollider. The counterpart is a static BoxCollider without RigidBody (wall).
    The following graphic illustrates the problem.

    Untitled-1.png

    Cheers
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Kinematic + MovePosition doesn't react to collisions so you may move the rigidbody to any position. In order to prevent it to inter-penetrate other objects, you may first detect the overlap and then use ComputePenetration to find a non-overlapping position. Check out:

    Physics.OverlapBox
    Physics.ComputePenetration
    Rigidbody.SweepTest (and its "All" variant)