Hand tracking in moving objects

76 Views Asked by At

I have a Unity network application that tracks my hands with the Vive Focus. I get the hands position of the rig with the following code:

if (hand.GetJoint(XRHandJointID.BeginMarker).TryGetPose(out Pose poseOut))
{
   handPosition = poseOut.position;
   handRotation = poseOut.rotation;    
}
 
if (HandTracking.subsystem.leftHand.Equals(hand))
   SendLeftHandPositionToServer(handPosition, handRotation);
else
   SendRightHandPositionToServer(handPosition, handRotation);

poseOut is the global position of my hands relative to the center of the scene. I need this position to set my avatar's hand to show it to the other network users. I send "handPosition" to the server and set the avatar's hand like this:

[ServerRpc]
private void SendRightHandPositionToServer(Vector3 rightHandPosition, Quaternion rightHandRotation)
{
   _avatarData.RootRightHand.position = rightHandPosition;
   _avatarData.RootRightHand.rotation = rightHandRotation;
 
   SendRightHandPositionToClient(rightHandPosition, rightHandRotation);
}

The problem is when my rig enters a moving object, like for example an elevator. I parent the rig to the moving object and my rig starts moving based on its behaviour. Because this movement is not registered by the rig, because I didn't move in real space, my avatar's hands stay in the starting point of the moving object. Is there a way to fix this problem?

If you need more context or code feel free to ask.

1

There are 1 best solutions below

0
On

I found the solution! XROrigin was always (0, 0, 0) in the build because it "resets" as I move through the real world space. The moment I enter the moving platform, it tracks the movement of the moving object (that is, the movement I have not done in real world). So I only had to do this:

[ServerRpc]
private void SendRightHandPositionToServer(Vector3 rigOrigin, Vector3 handPosition, Quaternion handRotation)
{
   SendRightHandPositionToClient(rigOrigin, handPosition, handRotation);
}

[ObserversRpc(BufferLast = true)]
private void SendRightHandPositionToClient(Vector3 rigOrigin, Vector3 handPosition, Quaternion handRotation)
{
   _avatarData.RootRightHand.position = handPosition + rigOrigin;
   _avatarData.RootRightHand.rotation = handRotation;
}

where rigOrigin is XROrigin.transform.position. Thanks for the help :)