1
\$\begingroup\$

Project Summary

Creating a multiplayer HoloLens app that allows players to spawn and move objects around the local space they share.

Problem

I am able to connect to the other HoloLens and see the other player in the correct location but, for some reason the objects that I spawn I offset. I determined that the offset was based off of where the application started off. When developing in HoloLens the device always starts at (0,0,0). However, once the client has connected to the host and imported their world anchor the client's coordinate system is never updated event though the anchor is in the same physical location. This creates a problem because if the client is offset by 180 degrees everything that the host does is offset by the same amount when moving an object if that makes sense.

Moving toward a solution

After doing a lot of research I found this pertaining to sharing and HoloLens:

"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."

I found this on Microsoft's website here

https://developer.microsoft.com/en-us/windows/mixed-reality/shared_experiences_in_unity

Question

So obviously my question is basically, is there a way to update the x,y,z coordinates of (0,0,0) during runtime?

\$\endgroup\$
  • \$\begingroup\$ This sounds like a bug that needs to be reported, not a failure on your part to do something. \$\endgroup\$ – Draco18s Sep 25 '17 at 19:56
  • \$\begingroup\$ It may be, but is there a way to update the Unity coordinate system during runtime? \$\endgroup\$ – Dtb49 Sep 25 '17 at 20:05
  • \$\begingroup\$ Are you using Spatial Anchors in a Room Scale environment? developer.microsoft.com/en-us/windows/mixed-reality/… \$\endgroup\$ – MichaelHouse Sep 25 '17 at 20:24
  • \$\begingroup\$ For testing purposes I am in a small room but for actual use it will be in a big space \$\endgroup\$ – Dtb49 Sep 25 '17 at 20:37
  • \$\begingroup\$ @Dtb49 I'm not talking about your literal environment. RoomScale is a TrackingSpaceType, used for configuring the coordinate system in Unity. \$\endgroup\$ – MichaelHouse Sep 25 '17 at 21:16
0
\$\begingroup\$

In my object controller script, I added a command that gets the position based off of the shared anchor 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 (receivedAuthority)
    {
        Vector3 objDir = transform.forward;
        Vector3 objPos = transform.position + objDir * .01f;

        localPosition = sharedAnchorTrans.InverseTransformPoint(objPos);
        // localRotation = transform.localRotation;
        CmdTransform(localPosition);
    }
    else if(!receivedAuthority)
    {
        transform.localPosition = localPosition;
        // transform.localRotation = localRotation;
    }
}

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

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

[Command]
public void CmdTransform(Vector3 position)
{
    if (!isLocalPlayer)
    {
      localPosition = position;
      //localRotation = rotation;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Don't forget to come back and mark this answer as "accepted" :) \$\endgroup\$ – Vaillancourt Sep 26 '17 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.