When using TeleportTo continuously to create an alternate locomotion system, the player may spin out of control.
Here’s a bit of knowledge that has been known for years through word of mouth, but as far as I could tell, I haven’t found articles online that fully explain it. It’s as if people are supposed to figure out how TeleportTo (U#)
’s AlignRoomWithSpawnPoint (U#)
(which is missing from non-U# docs) and TrackingDataType.Origin (U#)
(Docs) are supposed to work together just by looking at their individual descriptions.
This is because calling the function in the intuitive way by passing Networking.LocalPlayer.GetRotation()
, there is no guarantee that the rotation of the player after the teleportation will be equal to the rotation of the teleportation.
In order to teleport the player to the same rotation, you need to:
VRC_SceneDescriptor.SpawnOrientation.AlignRoomWithSpawnPoint
which will teleport the center of the player’s room instead of teleporting the player, andNetworking.LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Origin)
to find the current center of the player’s room in the worldYou can directly use the following function in your project:
/*
This code snippet is released into the public domain.
If the public domain is not applicable where you are, then it's released under the terms of
the CC0 1.0 Universal, which is essentially the same as public domain.
*/
// Teleporting to self should always result in the player not moving
StableTeleportTo(Networking.LocalPlayer.GetPosition());
// Teleport to other position
public Vector3 destinationPos = ...;
StableTeleportTo(destinationPos);
private static void StableTeleportTo(Vector3 destinationPos)
{
var playerPos = Networking.LocalPlayer.GetPosition();
var playerOrigin = Networking.LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Origin);
var posToOrigin = playerOrigin.position - playerPos;
var destinationOrigin = destinationPos + posToOrigin;
Networking.LocalPlayer.TeleportTo(
destinationOrigin,
playerOrigin.rotation,
VRC_SceneDescriptor.SpawnOrientation.AlignRoomWithSpawnPoint
);
}
This function is designed for continuous teleportation, so it does not accept any rotation parameters.
It calculates where the center of player’s room will be in such a way that the player will be located at the destination, and teleports the center of the player’s room to that location.