- Home /
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.
Answer by AltaNox · Jan 16, 2020 at 06:16 PM
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
This will not work with a rigidbody, unless you discard the position and only use the velocity. Even then, it's not supposed to be used for this.
It's good etiquette to explain with detail before down voting someone. I don't get what you mean by saying unless you discard the position and only use velocity. I have used this code in the past and it works as intended. $$anonymous$$aybe you what you don't get is the intention here. Also why is it not supposed to be used for this ?
I'm sorry, my comment was inadequate, and the downvote a mistake, based on a misunderstanding of your code. I unfortunately cannot remove it now.
Since smoothdamp returns a position, if you use it you have to set a rigidbody's position. If it's kinematic, that's alright, you can use $$anonymous$$ovePosition as the commentor asked, however he appears to be using a dynamic rigidbody.
If it is dynamic, you shouldn't use $$anonymous$$ovePosition, and only set the velocity. You can use the velocity variable of smoothdamp to input and modify a rigidbody's velocity, which works. But you should not set the position directly.
Answer by lgarczyn · Jan 17, 2020 at 01:23 AM
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;
This works however it is very slow even if i increase the speed a lot
Small mistake on my part, try the updated code
Also remember to use FixedUpdate for the code, and you might want to enable interpolation. If everyone works, please mark the question as solved.
Answer by JasonBennett · Jan 17, 2020 at 08:45 PM
Hi @sharktornado1,
First of all, put it in FixedUpdate. (Source: https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html) 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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612221635im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What's the best move/rotate method to simulate a herd of animals moving around? 2 Answers
transform.forward not always forward? 2 Answers
How do i add velocity in direction depending on WASD keys pressed 1 Answer
How good is to put Move.Towards on Fixed Update? -1 Answers
Is it ok to use transform.Rotate with a rigidbody that will use force also 2 Answers