Fetch Player Transform Object or determine Forward Vector

I am attempting to synchronize the view and position of a player to a camera in a different area. To properly sync the direction of the camera with the player, I need to reference Transform.forward to determine the “forward” vector of the player.

The issue is that I can’t get a transform object for the player. You can get the position and rotation separately using VRCPlayerApi.Position and VRCPlayerApi.Rotation, but those both provide a Vector3, not the base transform object.

Boils down to 2 questions:

Is there a way to get the base Transform object for a player?
Is there a way to calculate Transform.Forward only using VRCPlayerApi properties?

Self-answered Question:

Am I using the right position/rotation data, or should I be using VRCPlayerApiTrackingData?

Answer: Yes, using VRCPlayerApi.Position/Rotation provides the position and rotation of the player object itself, not the player’s head (where they would be looking from).

Unity doc on Transform.Forward: Unity - Scripting API: Transform.forward

For reference, I am trying to copy the update method in this video. The code is around the 11:10 mark. Smooth PORTALS in Unity - YouTube

I know this is fairly old, but maybe others will stumble across this too.

If you want to get the forward direction of the player capsule (not the head), you can use the

var playerForward = Networking.LocalPlayer.GetRotation() * Vector3.forward;

If you want to get the head - you can use
Networking.LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Head);

To get the forward of that you should be able to just use it like this

var tData = Networking.LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Head);
var forward = tData.rotation * Vector3.forward;

You can also just do smth like

public override void PostLateUpdate()
{
  var tData = Networking.LocalPlayer.GetTrackingData(VRCPlayerApi.TrackingDataType.Head);
  transform.SetPositionAndRotation(tData.position, tData.rotation);
}

or the same, but with the Networking.LocalPlayer.GetPosition/GetRotation

that way you’ll always have a single object that follows the player’s head / player base, then you can just reference it in any script that requires the forward direction, and use it’s transform.forward or any other direction

I used the PostLateUpdate event to make sure we’re getting the most up to date tracking data

1 Like