Move the Player
Teleport the player, or walk them to a new position inside your scene
To change the player's position in the scene, use the movePlayerTo() function. Use it to respawn players, set up checkpoints, or make a player walk to a spot as part of a cutscene.
This function takes an object with the following properties:
newRelativePosition: Where to position the player, expressed as a Vector3.cameraTarget: (optional) The direction to make the camera face, expressed as a Vector3 representing the coordinates of a point in space to look at. If no value is provided, the camera will maintain the same rotation as before moving.avatarTarget: (optional) The direction to make the avatar face, expressed as a Vector3 representing the coordinates of a point in space to look at. If no value is provided, the avatar will maintain the same rotation as before moving. If the player is in 1st person camera mode, the camera and avatar rotation are the same.duration: (optional) How long the transition should take, in seconds. If no value is provided, the transition will occur instantly. If a duration is provided, the avatar will walk or run to this new position.
import { movePlayerTo } from '~system/RestrictedActions'
// create entity
const myEntity = engine.addEntity()
MeshRenderer.setBox(myEntity)
MeshCollider.setBox(myEntity)
Transform.create(myEntity, {
position: { x: 4, y: 1, z: 4 },
})
// give entity behavior
pointerEventsSystem.onPointerDown(
{
entity: myEntity,
opts: { button: InputAction.IA_POINTER, hoverText: 'Click' },
},
function () {
// respawn player
movePlayerTo({
newRelativePosition: Vector3.create(1, 0, 1),
cameraTarget: Vector3.create(8, 1, 8),
avatarTarget: Vector3.create(8, 1, 8),
})
}
)The player's movement occurs instantly, without any confirmation screens or camera transitions.
The movePlayerTo() function is awaitable, so if the movement has a duration, you can use await to wait for the player to reach their destination.
If the player attempts to move during the duration of their transition, the transition will be interrupted and the movePlayerTo function will never resolve. You can prevent this behavior by disabling all inputs while the transition is in progress using the InputModifier component, see Restrict locomotion.
Last updated