Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

AddForce() causing objects to intersect for one frame

Discussion in 'Physics' started by rstern, Dec 5, 2018.

  1. rstern

    rstern

    Joined:
    Nov 26, 2015
    Posts:
    2
    I made a simple project to test different ways of moving rigid bodies and found some odd results:

    I have a cube that the player can move around with a Box Collider and a Rigid Body, set to Continuous Dynamic.
    I have a wall that has only a Box Collider on it.

    My movement code uses this in the fixed update:
    m_RigidBody.AddForce(m_m3Velocity * Time.fixedDeltaTime, ForceMode.Impulse);

    When I move the cube at high speed, it visibly penetrates half way into the wall for one frame before being pushed back. I can pause Unity just before the moment of impact and step forward frame by frame and actually catch the frame where it intersects.

    My understanding was that using AddForce() with a Continuous Dynamic rigid body should not penetrate at all: it uses swept collision doesn't it and should resolve the penetration before rendering.
    How do you prevent this bug?
     
  2. rstern

    rstern

    Joined:
    Nov 26, 2015
    Posts:
    2
    With further testing, I can reproduce this most accurately using this code in the fixed update:

    Code (CSharp):
    1. Vector3 currentVelocity = m_RigidBody.velocity;
    2. currentVelocity.x = 30;
    3. m_RigidBody.velocity = currentVelocity;
    Using the editors Pause and Step buttons, I can freeze it on the frame where a 1m cube penetrates to half its depth for one frame before being pushed back out.