transform.Translate vs rigidbody.MovePosition?

来源:互联网 发布:以前网络歌手名单大全 编辑:程序博客网 时间:2024/05/18 04:50


【在NOT kinematic的情况下,rigidbody.MovePosition考虑了物理引擎的东西,比如物体之间的碰撞。直接设置rigidbody的Postion,更像是是做一个传送或者瞬移。rigidbody.MovePosition更像是做跑动,或者走路。


The reason why it is recommended to use MovePosition is because when the rigid body is NOT kinematic, an extra step is taken into account when calculating how the rigid body reacts to physics. Since the transform has nothing to do with physics, it is just the coordinate information of the NON physics based components of the Game Object, this causes variation between the two bodies.

Now MovePosition takes this into effect and more properly handles your transform information along with your rigid body information.

When you think about it, when translating the transform and not the rigid body itself in the case of NON-kinematic bodies, the rigid body information is thus calculated at the end of the physics step frame, causing a slight difference in the two positions of each body. It's pretty much the same thing if you translate the position of the rigid body using
 rigidbody.position or anything of the like.MovePosition was made specifically to deal with this oscillation of the two bodies in conjunction with active physics. CPU overhead is negligible in this case, since MovePosition was made specifically for this purpose.


Rigidbody.MovePosition
public void MovePosition(Vector3 position);

Moves the rigidbody to position.
Use Rigidbody.MovePosition to move a Rigidbody, complying with the Rigidbody's interpolation setting.

If Rigidbody interpolationis enabled on the Rigidbody, calling Rigidbody.MovePosition results in a smooth transition between the two positions in any intermediate frames rendered. This should be used if you want to continuously move a rigidbody in each FixedUpdate.

Set Rigidbody.position instead, if you want to teleport a rigidbody from one position to another, with no intermediate positions being rendered.

using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour {    public Vector3 teleportPoint;    public Rigidbody rb;    void Start() {        rb = GetComponent<Rigidbody>();    }    void FixedUpdate() {        rb.MovePosition(transform.position + transform.forward * Time.deltaTime);    }}

If the rigidbody has isKinematic set false then it works differently. It works like transform.position=newPosition and teleports the object (rather than a smooth transition).


0 0
原创粉丝点击