> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/tou-xiang/move-player.md).

# 移动玩家

将玩家传送到场景中的新位置，或引导其步行到新位置

要更改玩家在场景中的位置，请使用 `movePlayerTo()` 函数。你可以用它让玩家重生、设置检查点，或在过场动画中让玩家走到某个位置。

{% hint style="info" %}
**💡 提示**：移动玩家最简单的方法是使用 [Creator Hub 中的场景编辑器](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-bian-ji-qi/ru-men/about-editor.md)。使用免代码的 **移动玩家** 或 **将玩家移动到此处** 操作，参见 [让任意物品变得智能](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-bian-ji-qi/jiao-hu-xing/make-any-item-smart.md).
{% endhint %}

此函数接受一个包含以下属性的对象：

* `newRelativePosition`：玩家要移动到的位置，以 Vector3 表示。
* `cameraTarget`：（可选）摄像机朝向，以 Vector3 表示，代表空间中目标点的坐标。如果未提供此值，摄像机将保持移动前的旋转角度。
* `avatarTarget`：（可选）虚拟形象朝向，以 Vector3 表示，代表空间中目标点的坐标。如果未提供此值，虚拟形象将保持移动前的旋转角度。如果玩家处于第一人称摄像机模式，摄像机和虚拟形象的旋转角度相同。
* `duration`：（可选）过渡所需的时长，单位为秒。如果未提供此值，过渡将立即完成。如果提供了时长，虚拟形象将步行或奔跑到新位置。

{% hint style="warning" %}
**📔 注意**：过渡期间，虚拟形象不受碰撞体影响，因此可以穿过物体。
{% endhint %}

```ts
import { movePlayerTo } from '~system/RestrictedActions'

// 创建实体
const myEntity = engine.addEntity()
MeshRenderer.setBox(myEntity)
MeshCollider.setBox(myEntity)

Transform.create(myEntity, {
	position: { x: 4, y: 1, z: 4 },
})

// 为实体赋予行为
pointerEventsSystem.onPointerDown(
	{
		entity: myEntity,
		opts: { button: InputAction.IA_POINTER, hoverText: 'Click' },
	},
	function () {
		// 让玩家重生
		movePlayerTo({
			newRelativePosition: Vector3.create(1, 0, 1),
			cameraTarget: Vector3.create(8, 1, 8),
			avatarTarget: Vector3.create(8, 1, 8),
		})
	}
)
```

玩家会立即移动，不会显示确认屏幕或发生摄像机过渡。

{% hint style="warning" %}
**📔 注意**：只有当玩家已经身处场景边界内时，才能移动玩家；而且玩家只能移动到场景边界范围内的位置。你不能使用 `movePlayerTo()` 将玩家传送到另一个场景。要将玩家移动到另一个场景，请参见 [传送](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/external-links.md#teleports).
{% endhint %}

movePlayerTo() `函数支持 await，因此如果移动需要一定时长，可以使用` 函数支持 await，因此如果移动需要一定时长，可以使用 `await` 等待玩家到达目的地。

```ts
import { movePlayerTo } from '~system/RestrictedActions'

pointerEventsSystem.onPointerDown(
	{
		entity: myEntity,
		opts: { button: InputAction.IA_POINTER, hoverText: 'Click' },
	},
	async function () {
		await movePlayerTo({
			newRelativePosition: Vector3.create(1, 0, 1),
			cameraTarget: Vector3.create(8, 1, 8),
			avatarTarget: Vector3.create(8, 1, 8),
			duration: 2,
		})
	}
)
```

{% hint style="warning" %}
**📔 注意**：如果没有 `duration`，Promise 会在传送排入队列后立即完成，而不是等玩家抵达后才完成。此后的代码 `await` 运行期间 `Transform.get(engine.PlayerEntity)` 仍会报告旧位置，而稍后才完成的移动可能会中断你在目的地开始的表情动作。如果需要在抵达时执行某些操作，请改为轮询玩家的位置：

```ts
import { engine, Transform } from '@dcl/sdk/ecs'
import { Vector3 } from '@dcl/sdk/math'
import { movePlayerTo } from '~system/RestrictedActions'

function teleportThen(target: Vector3, onArrive: () => void) {
	void movePlayerTo({ newRelativePosition: target })

	let waited = 0
	function waitForArrival(dt: number) {
		waited += dt
		const pos = Transform.getOrNull(engine.PlayerEntity)?.position
		// 仅比较 X 和 Z，以免高度差导致等待一直无法结束
		const arrived = pos !== undefined && Math.hypot(pos.x - target.x, pos.z - target.z) < 0.2
		if (!arrived && waited < 1.5) return // 1.5 秒后放弃
		engine.removeSystem(waitForArrival)
		onArrive()
	}

	engine.addSystem(waitForArrival)
}
```

{% endhint %}

如果玩家在过渡期间尝试移动，过渡就会中断，且 `movePlayerTo` 函数将永远无法完成。你可以使用 `InputModifier` 组件，在过渡期间禁用所有输入，以避免这种情况。请参阅 [限制移动](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/tou-xiang/locomotion.md#restrict-locomotion).

```ts
import { movePlayerTo } from '~system/RestrictedActions'
import {InputModifier, engine} from '@dcl/sdk/ecs'

pointerEventsSystem.onPointerDown(
	{
		entity: myEntity,
		opts: { button: InputAction.IA_POINTER, hoverText: 'Click' },
	},
	async function () {

		// 禁用所有输入
		InputModifier.create(engine.PlayerEntity, {
			mode: InputModifier.Mode.Standard({
				disableAll: true,
			}),
		})

		// 移动玩家
		await movePlayerTo({
			newRelativePosition: Vector3.create(1, 0, 1),
			cameraTarget: Vector3.create(8, 1, 8),
			avatarTarget: Vector3.create(8, 1, 8),
			duration: 2,
		})

		// 启用所有输入
		InputModifier.deleteFrom(engine.PlayerEntity)
	}
)
```

{% hint style="warning" %}
**📔 注意**：此外，还可以保存 await 的结果，并检查其 `success` 属性，以判断移动是被中断（例如被输入操作打断），还是已成功完成。
{% endhint %}

{% hint style="info" %}
**💡 提示**：若要将玩家移动到其他场景，或 Genesis City 地图上的其他坐标，请参阅 [传送](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/external-links.md#teleports).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/tou-xiang/move-player.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
