What does Rigidbody.MovePosition do?

I wanted to create my own version of the Rigidbody.MovePosition function by doing this:

            void MovePos(Vector3 pos)
            {
            Vector3 dir = Vector3.RotateTowards(transform.forward,
            pos, 360, 100);
            Rb.velocity = dir;
            }

But it doesn’t work as expected.
I went to a discussion where someone said that Rigidbody.MovePosition “Moves the rigidbody to the specified position by calculating the appropriate linear velocity required to move the rigidbody to that position during the next physics update. During the move, neither gravity or linear drag will affect the body. This causes the object to rapidly move from the existing position, through the world, to the specified position.”

What would this calculation be in Unity?

Simulation happens in time-steps, by default Time.fixedDeltaTime. You’re not thinking about time at all.

var moveDelta = posTo - rb.position;
rb.velocity = moveDelta * Time.fixedDeltaTime;

This velocity for a single simulation time-step will move it to “posTo”. You need to calculate it again for the next simulation step.

Don’t forget though, if you’re doing this manually, this doesn’t stop gravity and drag from affecting it.

I disabled use gravity on the rigidbody so it won’t get affected by gravity.

MovePosition does work though so trying some alternative seems drastic. You should clarify what “doesn’t work as expected” means if you need further help.

1 Like