Search Unity

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. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    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:
    11,500
    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:
    2,512
    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.
     
  5. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    I'm facing a similar issue. The docs say "Rigidbody.position allows you to get and set the position of a Rigidbody using the physics engine." - So I assumed physics interactions are being preserved, but it doesn't look that way. I'm placing a small cube on top of a large one, using regular gravity. When I move the large cube via Rigidbody.position, the small one stays more or less in place, like it is sliding along the large body's surface, although I've set their material's friction to 1.