how to use rigidbody.moveposition to go to an exact location

If i have a vector 3 of a point where i want the character to smoothly move to, how do i use rigidbody.moveposition to achieve this?
I tried using the movetowards function but it doesnt work as well with rigidbody physics and jumping + collision. Can you also tell me whether i should code rigid body related things in fixed update or update.

There are multiple ways to make something move “smoothly” towards a point. If you have time constraints then a Lerp combined with an easing function would work well. If you don’t have time constraints you could try using the Vector3.SmoothDamp function. Something like this

``````body.MovePosition(Vector3.SmoothDamp(body.position,targetPoint,ref velocity,smoothTime);
``````

And yes, you should be doing this in FixedUpdate

The previous answer works for a kinematic rigidbody, here’s the solution for a dynamic rigidbody, which will not pass through walls.

``````  // Get the delta position
Vector3 dir = target - rb.position;
// Get the velocity required to reach the target in the next frame
dir /= Time.fixedDeltaTime;
// Clamp that to the max speed
dir = Vector3.ClampMagnitude(dir, speed);
// Apply that to the rigidbody
rb.velocity = dir;
``````

with rb being a script variable containing the your objects rigidbody, and target being the world position of your target.

If it is not smooth enough, you can try:

``````Vector3 velocity = rb.velocity;
Vector3.SmoothDamp(
rb.position,
target,
ref velocity,
timeToReachTarget,
maxSpeed,
Time.fixedDeltaTime);
rb.velocity = velocity;
``````

First of all, put it in FixedUpdate. (Source: Unity - Scripting API: Rigidbody.MovePosition) Secondly, it has to be a Kinetic Rigidbody.

If the Rigidbody has isKinematic set to false, it works like transform.position=newPosition and teleports the object to the new position (rather than performing a smooth transition).

So you need to have a Kinetic Rigidbody. Then you simply need to figure out the amount you want to move per frame.

``````float moveAmount = .25f;
Vector3 newPosition = transform.position + (new Vector3(1,0,0) * moveAmount);
RB.MovePosition(newPosition);
``````

The previous moves a Rigidbody to the right at the speed of moveAmount, which you can change as needed.

Collisions for Kinematic Rigidbody are tricky. They register on Dynamic Rigidbodies, but not on other Kinematic ones (as least in terms of their physics.) Let me know if this works and if you need more information.