Skip to content

Commit

Permalink
Fixed _TeleportTo() with rotation
Browse files Browse the repository at this point in the history
• Fixed _TeleportTo() overload that took a position, rotation and a bool since it didn't account for play space offsets.
• Made both desktop and VR use the AlignRomWithSpawnPoint SpawnOrientation when teleporting using _TeleportTo().
  • Loading branch information
Nestorboy committed Oct 18, 2023
1 parent b0ec847 commit da8baab
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Packages/com.nessie.udon.movement/Runtime/Scripts/NUMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -535,15 +535,29 @@ public virtual void _SetPosition(Vector3 position)
[PublicAPI]
public virtual void _TeleportTo(Vector3 position, bool lerpOnRemote = false)
{
_TeleportTo(position, LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Origin).rotation, lerpOnRemote);
_TeleportTo(position, LocalPlayer.GetRotation(), lerpOnRemote);
}

[PublicAPI]
public virtual void _TeleportTo(Vector3 position, Quaternion rotation, bool lerpOnRemote = false)
{
var orientation = InVR ? VRC_SceneDescriptor.SpawnOrientation.AlignRoomWithSpawnPoint : VRC_SceneDescriptor.SpawnOrientation.Default;
Vector3 playOffset = LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Origin).position - LocalPlayer.GetPosition();
_TeleportTo(position + playOffset, rotation, orientation, lerpOnRemote);
rotation = Quaternion.Euler(0, rotation.eulerAngles.y, 0);

Vector3 playerPos = LocalPlayer.GetPosition();
Quaternion playerRot = LocalPlayer.GetRotation();
Quaternion invPlayerRot = Quaternion.Inverse(playerRot);

var originData = LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Origin);
Vector3 originPos = originData.position;
Quaternion originRot = originData.rotation;

Vector3 posOffset = originPos - playerPos;
Quaternion rotOffset = invPlayerRot * originRot;

Quaternion targetRot = rotation * rotOffset;
Vector3 targetPos = position + invPlayerRot * rotation * posOffset;

_TeleportTo(targetPos, targetRot, VRC_SceneDescriptor.SpawnOrientation.AlignRoomWithSpawnPoint, lerpOnRemote);
}

[PublicAPI]
Expand Down

0 comments on commit da8baab

Please sign in to comment.