- Home /
How can I move a rigidbody to a desired position with interpolation?
Hi
I have a rigidbody player I want to move to a desired position. I don't want it to move it like you would normally have a player move. I just want to move it from Point A to Point B with interpolation, like using Vector3.Lerp. The reason I want to use the rigidbody instead of V3.Lerp is because the lerping slows down the closer it gets to the target and that messes with my code and the feel of of the player.
So this is the two ways I've tried doing this.
void FixedUpdate()
{
rig.MovePosition(movePosition * Time.deltaTime * moveSpeed);
}
The problem with this is that the player just gets stuck at around 0,0,0 and can not be moved. I have no idea what is going on when this happens.
Then I tried this.
void FixedUpdate()
{
rig.MovePosition(tr.position + movePosition * Time.deltaTime * moveSpeed);
}
The problem with this is that the player keeps walking endlessly since it is always adding the movePosition and transform position.
I have no idea what I am supposed to now.
Any help with this is greatly appreciated!
The reason I want to use the rigidbody ins$$anonymous$$d of V3.Lerp is because the lerping slows down the closer it gets to the target
No it doesn't. Vector3.Lerp just calculates a vector based on the parameters you give. The results it gives depend only on the parameters you put in. $$anonymous$$ost likely you are doing something like
transform.position = Vector3.Lerp(transform.position, x, Time.deltaTime)
It causes slowing down because if deltatime is let's say 0.05, the object moves 5% of the RE$$anonymous$$AINING distance every frame and the remaining distance gets smaller all the time.
Answer by Munchy2007 · Jan 03, 2016 at 06:02 PM
Use Vector3.MoveTowards() this will result in a constant speed movement from the start position to the target position.
http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html (substitute rigidbody.movePosition for transform.position in the script reference example).
You can compare your rigidbody.position with target position and when they are close enough you can stop moving.
Edit: Vector3.Lerp() can produce the same result as movetowards if it's used properly, i.e. not with Time.deltaTime as the 3rd parameter.
I ended up using Vector3.$$anonymous$$oveTowards. Thanks for the help!
Works better as $$anonymous$$ovePosition, but it rotates on target to original rotation.
Do you know how to fix it?? Thanks
Answer by MrFloxy · Jun 27, 2021 at 10:44 AM
unfortunately, your transformation method is not correct, since objects move in jerks, I would like to get a smooth movement of objects using physics