返回介绍

Rigidbody2D.MovePosition 移动位置

发布于 2019-12-18 15:38:30 字数 3331 浏览 1243 评论 0 收藏 0

JavaScript => MovePosition(position: Vector2): void;
C# => void MovePosition(Vector2 position);

Parameters 参数

positionThe new position for the Rigidbody object.
用于该刚体对象的新位置。

Description 描述

Moves the rigidbody to position.

移动刚体到position。

Moves the rigidbody to the specified position by calculating the appropriate linear velocity required to move the rigidbody to that position during the next physics update. During the move, neither gravity or linear drag will affect the body. This causes the object to rapidly move from the existing position, through the world, to the specified position.

移动该刚体到指定的position,通过合适的线速度计算来移动刚体到指定的位置,该位置在下次物理更新。在移动期间,没有重力或线阻力影响刚体。这使得对象从现有的位置快速移动到通过世界坐标来指定的“position”。

Because this feature allows a rigidbody to be moved rapidly to the specified position through the world, any colliders attached to the rigidbody will react as expected i.e. they will produce collisions and/or triggers. This also means that if the colliders produce a collision then it will affect the rigidbody movement and potentially stop it from reaching the specified position during the next physics update. If the rigidbody is kinematic then any collisions won't affect the rigidbody itself and will only affect any other dynamic colliders.

因为这个功能允许刚体通过世界坐标快速移动到指定的位置,附加到刚体的任何碰撞器正如预期反应;如,产生碰撞或触发。这意味着如果刚体产生碰撞,那么在它到达指定的“position”下次物理更新期间,将影响刚体移动或可能停止。如果是运动学刚体,那么任何碰撞不会影响刚体自身,仅影响其他物理碰撞。

2D rigidbodies have a fixed limit on how fast they can move therefore attempting to move large distances over short time-scales can result in the rigidbody not reaching the specified position during the next physics update. It is recommended that you use this for relatively small distance movements only.

2D刚体有一个固定的限制,它们快速移动,因此在很短的时间试图移动很大的距离,这导致刚体在下次物理更新期间没能到达指定的“position”。建议你只在使用相对较小的距离移动。

It is important to understand that the actual position change will only occur during the next physics update therefore calling this method repeatedly without waiting for the next physics update will result in the last call being used. For this reason, it is recommended that it is called during the FixedUpdate callback.

重要的是要了解实际位置变化只发生在下次物理更新,因此,调用这个方法不等待下次物理更新将导致上次调用正在使用。由于这个原因,建议在FixedUpdate回调时调用。

JavaScript:

private var speed : Vector2 = Vector2 (3, 0);
	function FixedUpdate () {
		rigidbody2D.MovePosition(rigidbody2D.position + speed * Time.deltaTime);
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    private Vector2 speed = new Vector2(3, 0);
    void FixedUpdate() {
        rigidbody2D.MovePosition(rigidbody2D.position + speed * Time.deltaTime);
    }
}

Rigidbody2D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文