Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Moving rigidbody to position, rigidbody goes throught colliders

Discussion in 'Physics' started by Etwus, Nov 4, 2019.

  1. Etwus

    Etwus

    Joined:
    Dec 12, 2016
    Posts:
    18
    I need rigidbody to move from current position to new position. I tried it using RigidBody.MovePosition, the problem is that the moved object goes throught colliders when there is big difference between current and target position. I am updating it in fixed update.

    Code (CSharp):
    1. movedObject.GetComponent<RigidBody>().MovePosition(target)
    How should I do this, so the rigidbody can't go throught collider?
     
  2. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,918
    What is the interpolation setting on the body?
    I think you should be set to interpolate, if it's set to this and extrapolate(and any other one that's there) this should be a bug according to the docs.
    "Rigidbody.MovePosition moves a Rigidbody and complies with the the interpolation settings. When Rigidbody interpolation is enabled, Rigidbody.MovePosition creates a smooth transition between frames."
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,004
    To be clear on this subject; Interpolation is just a visual thing, it's not used in collision detection and only updates the Transform on the GameObject per-frame. The actual body just sits in its new position while the Transform interpolates from its last position to the new one.

    It's more likely related to the fact that the Rigidbody is set to use discrete collision-detection therefore it's just tunneling past colliders.
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,571
    I don't think this is possible using MovePosition. You're just forcing the object to move to the target position, then to continue the physics simulation from there.

    Reacting to colliders properly require you to move the object using Rigidbody.AddForce and/or Rigidbody.AddForceAtPosition only. Note that these methods allow to specify mass-independent accelerations and velocity deltas. Check out the ForceMode parameter.