- Home /
Rigidbody MovePosition clipping
When I use Rigidbody.MovePosition and the object being moved hits another Rigidbody, the object will overlap the other slightly until the MovePosition is released (eg, moving the player, if a direction is held "into" a wall, the player will clip into the wall very slightly, then slide back out when the direction is released.
I am using FixedUpdate, Box Colliders and non-kinematic rigidbodies.
public class MoveController : MonoBehaviour {
public float walkSpeed = 4f;
public float acceleration = 5f;
public float deceleration = 20f;
Rigidbody rb;
float currentSpeed;
Vector3 movement;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVertical = Input.GetAxisRaw("Vertical");
if (moveHorizontal == 0f && moveVertical == 0f) {
currentSpeed = Mathf.Lerp(currentSpeed, 0f, deceleration * Time.deltaTime);
}
else {
currentSpeed = Mathf.Lerp(currentSpeed, walkSpeed, acceleration * Time.deltaTime);
}
movement.Set(moveHorizontal, 0.0f, moveVertical);
movement = movement.normalized * currentSpeed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
}
This gif demonstrates the clipping behaviour:
How do I prevent this? It particularly causes problems when an object in mid-air collides with a wall, it will "stick" to it and stop falling until there is no horizontal push being applied.
I'm having the same issue described above.
I have a Sphere with a Rigidbody; the Sphere follows the mouse position via Rigidbody.$$anonymous$$ovePosition.
It works correctly when colliding with another Sphere with a Rigidbody - the second Sphere is pushed around with no overlap. When colliding with unmovable objects it will move inside the object while being pushed back a little, and clip right through if it reaches the center point.
have you tried changing the collision detection setting in the RigidBody component to Continuous or Continuous Dynamic? Also try setting Interpolate to "Interpolate" ins$$anonymous$$d of "none"
$$anonymous$$ovePosition is used on kinematic Rigidbodies. In your case, change the velocity and the problem will be gone.
@kornstar83 interpolate and collision detection settings don't appear to have any effect on this behaviour
@hexagonius That fixes the overlap issue, but still causes the moving object to stick to walls if it collides in mid-air. The moving object will still clip into the wall for a frame too on collision
Are you using continuous collision detection?
Answer by Naggil · Nov 19, 2019 at 09:39 PM
use rb.velocity(movement);
instead
I think that in most cases its better to not use move position to move a rigidbody.