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. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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.