- Home /
Question by
PaperMartin1 · Jan 21, 2020 at 08:41 PM ·
collisionphysicsrigidbodycontrolsmoveposition
is there a way to move a rigidbody with something like moveposition without clipping through collider?
I want my character to have a rigidbody that's affected by external forces, but don't wanna use forces to move it cause then it kinda sucks to get the movements to feel right,and drag fucks with gravity
Comment
Best Answer
Answer by lgarczyn · Jan 23, 2020 at 04:31 AM
Sorry, MovePosition for a non-kinematic rigidbody is exactly the same as setting rigidbody.position, which ignores all colliders.
You don't want to use MovePosition every frame anyway, as your rigidbody would simply not react to any external forces.
You can code your own rudimentary MovePosition that still allows gravity, collisions and impacts:
void MovePosition(Vector3 position)
{
Vector3 oldVel = rb.velocity;
//Get the position offset
Vector3 delta = position - rb.position;
//Get the speed required to reach it next frame
Vector3 vel = delta / Time.fixedDeltaTime;
//If you still want gravity, you can do this
vel.y = oldVel.y;
//If you want your rigidbody to not stop easily when hit
//This is however untested, and you should probably use a damper system instead, like using Smoothdamp but only keeping the velocity component
vel.x = Mathf.Abs(oldVel.x) > Mathf.Abs(vel.x) ? oldVel.x : vel.x;
vel.z = Mathf.Abs(oldVel.z) > Mathf.Abs(vel.z) ? oldVel.z : vel.z;
rb.velocity = vel
}
Or you can just use the CharacterController and the Move function