This code works. ↓
private void FixedUpdate()
{
Vector3 newPosition = new Vector3(0, 1, 0);
GetComponent<Rigidbody2D>().MovePosition(newPosition);
}
This code not works. ↓
private void FixedUpdate()
{
Vector3 newPosition = new Vector3(0, 1, 0);
Vector3 oldPosition = transform.position;
transform.position = newPosition;
transform.position = oldPosition;
GetComponent<Rigidbody2D>().MovePosition(newPosition);
}
This code works. ↓
private void FixedUpdate()
{
Vector3 newPosition = new Vector3(0, 1, 0);
Vector3 oldPosition = transform.position;
transform.position = newPosition;
transform.position = oldPosition;
Physics2D.SyncTransforms(); //newly added
GetComponent<Rigidbody2D>().MovePosition(newPosition);
}
I don’t quite understand Rigidbody2D.MovePosition().
Why does it stop functioning when transform.position is changed? (And it gives no warning.)
What is the rule for Rigidbody2D.MovePosition()? When will it function, when not?
(I also tested Rigidbody2D.velocity. it seems Rigidbody2D.velocity always perform no matter transform.position is changed or not.)
I know it is suggested not to move rigidbody by Transform.
Sometimes the designed movement is a bit different from real world, while physics2d engine always do the move like real world.
Besides, physics2d engine only takes place in internal physics update.
There are chances I want to move rigidbody by Transform.