Update Coordinates for Sharing World Anchor Hololens

I am currently trying to spawn an object and allow players to spawn and move objects around the scene. I am able to anchor the players, spawn the objects, and move the objects. However, I found an issue where the Unity coordinate system does not update once I anchor a client. The anchor is in the correct physical location in space but the coordinate system is off. For example, depending on where I run the application that is considered the (0,0,0) spot for the client and does not update once it is connected to the host but from the host’s perspective the client is at (1,0,2).

I found this on Microsoft’s website when looking into sharing:

“After a GameObject is locked via the LockObject call, it will have a WorldAnchor which will keep it in the same physical position in the world, but it may be at a different location in the Unity coordinate space than other users.”

Is there a way around this or a way to update the Unity coordinate system?

What do you mean by anchoring the players? Are you attaching WorldAnchor objects to the players?

So, when another player joins via Unet their coordinate system does not change to match the host. They retain their own coordinate system but the anchor is put in the correct location relative to the players.

I finally figured out a way around this. In my object controller script, I added a command that gets the position based off of the shared anchor position which is in the correct position for both players. My code looks a little bit like this. I hope this helps anyone else that comes across this problem.

/// The position relative to the shared world anchor.
[SyncVar]
private Vector3 localPosition;

///The transform of the shared world anchor
Transform sharedAnchorTrans;

/// The rotation relative to the shared world anchor.
[SyncVar]
private Quaternion localRotation;

void Start()
{
   //Get the instance of your anchor in the world this will be different
   sharedAnchorTrans = SharedCollection.Instance.gameObject.transform;

   // I set this to be the child of an object with an anchor so that I do not have to
   // have one on this object because it takes up a lot of processing power to have
   // multiple anchors.
   transform.SetParent(SharedCollection.Instance.transform, false);
}

private void Update()
{
//if we are the one moving the object do this
   if (receivedAuthority)
   {
       Vector3 objDir = transform.forward;
       Vector3 objPos = transform.position + objDir * .01f;

       localPosition = sharedAnchorTrans.InverseTransformPoint(objPos);
       // localRotation = transform.localRotation;
       CmdTransform(localPosition);
   }
//else we are not moving it or someone else is moving the object do this
   else if(!receivedAuthority)
   {
       transform.localPosition = localPosition;
       // transform.localRotation = localRotation;
   }
}

public override void OnStartAuthority()
{
   receivedAuthority = true;
}

public override void OnStopAuthority()
{
   receivedAuthority = false;
}

//tell all the clients what the updated position is
[Command]
public void CmdTransform(Vector3 position)
{
   if (!isLocalPlayer)
   {
     localPosition = position;
     //localRotation = rotation;
   }
}
1 Like

thanks for sharing. this was really helpful!