- Home /
Rigidbody.MovePosition not behaving as expected (possible bug?)
My scene is simple. There's a cube with a Rigid Body component attached. Here's all the info on it's Rigid Body component (from the inspector):
Mass = 1
Drag = 0
Angular Drag = 0.05
Use Gravity = (False)
IsKinematic = (True)
Interpolate = Interpolate
Collision Detection = Discrete
No Constraints
There's another cube in the scene without a Rigid body. This cube will simply act as a destination point for the first cube.
I want the first cube to smoothly move towards the destination cube's position using the Rigidbody.MovePosition function. Here's the code I used on the first cube:
public class CubeMotion : MonoBehaviour
{
private Rigidbody rb;
public float speed;
public Transform destinationPoint;
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate ()
{
rb.MovePosition (destinationPoint.position * Time.fixedDeltaTime * speed);
}
}
However, the cube doesn't move to it's destination point as expected. It simply teleports to it's destination point. I even tried changing the destinationPoint variable from a Transform to a Vector3 (as the manual suggests):
private Rigidbody rb;
public float speed;
public Transform des;
public Vector3 destinationPoint;
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate ()
{
destinationPoint = des.position;
rb.MovePosition (destinationPoint * Time.fixedDeltaTime * speed);
}
Since the destination cube is located at coordinates 0,0,0, I even tried this:
rb.MovePosition (new Vector3(0,0,0) * Time.fixedDeltaTime * speed);
None of them worked. I even tried playing around with the main cube's Rigid body values (like setting IsKinematic to "false" and setting Interpolate to "none"). They all lead to the same result: The first cube simply teleporting to the position of the destination cube, instead of the smooth transition I was expecting. I know I can achieve better results by using Vector3.MoveTowards() function, but I'm not going to do that. I want the cube's movement to be entirely based on physics (hence Rigidbody functions).
So, am I doing something wrong? Or could this be a bug in Unity? I'm using Unity version 5.5.1 (64-bit) .
Answer by GrogmanEsquire · Jun 08, 2018 at 05:54 PM
Vector3 direction = (destinationPoint - transform.position).normalized;
rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
This is what you want to do, rather than input the destination coordinates, you want to input the current coordinates with the addition of a directional vector. you can use a Speed float to alter the magnitude of your directional vector.
the way MovePosition() works, when it is called it moves the rigidbody from where it is, to the Vector3 that is fed into it. so inputting the final destination directly and multiplying it, immediately teleports the rigidbody to that position. in order to have it step over time, every fixedupdate you want to poll the current position, and move along a directional vector toward the destination. so for example if every fixedupdate you wanted to move your rigidbody left, you would want to add a vector (-1,0,0) then you would modify that by your speed and time variables to control the rate each fixedupdate.
rb.MovePosition((transform.position + new Vector3(-1, 0, 0)) * speed * Time.deltaTime);
every FixedUpdate it would move the object -1*speed*time.deltatime from its current position.
I know this is a bit of a necro on an old question but figured I'd put up the answer just in case someone else stumbled across it.
Your answer
Follow this Question
Related Questions
Rigidbody.MovePosition/MoveRotation Hits far away colliders 2 Answers
Calling Rigidbody.MovePosition, no movement at all 4 Answers
My Player goes through the colliders even I used Rigidbody.MovePosition , Please help me !! 2 Answers
Is it possible to prevent parent rigidbody physics from effecting children rigidbodies? 1 Answer