Search Unity

Update Coordinates for Sharing World Anchor Hololens

Discussion in 'VR' started by dbarrett, Sep 25, 2017.

  1. dbarrett

    dbarrett

    Joined:
    Sep 6, 2017
    Posts:
    32
    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?

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

    unity_andrewc

    Unity Technologies

    Joined:
    Dec 14, 2015
    Posts:
    220
    What do you mean by anchoring the players? Are you attaching WorldAnchor objects to the players?
     
  3. dbarrett

    dbarrett

    Joined:
    Sep 6, 2017
    Posts:
    32
    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.

    Code (CSharp):
    1. /// The position relative to the shared world anchor.
    2. [SyncVar]
    3. private Vector3 localPosition;
    4.  
    5. ///The transform of the shared world anchor
    6. Transform sharedAnchorTrans;
    7.  
    8. /// The rotation relative to the shared world anchor.
    9. [SyncVar]
    10. private Quaternion localRotation;
    11.  
    12. void Start()
    13. {
    14.    //Get the instance of your anchor in the world this will be different
    15.    sharedAnchorTrans = SharedCollection.Instance.gameObject.transform;
    16.  
    17.    // I set this to be the child of an object with an anchor so that I do not have to
    18.    // have one on this object because it takes up a lot of processing power to have
    19.    // multiple anchors.
    20.    transform.SetParent(SharedCollection.Instance.transform, false);
    21. }
    22.  
    23. private void Update()
    24. {
    25. //if we are the one moving the object do this
    26.    if (receivedAuthority)
    27.    {
    28.        Vector3 objDir = transform.forward;
    29.        Vector3 objPos = transform.position + objDir * .01f;
    30.  
    31.        localPosition = sharedAnchorTrans.InverseTransformPoint(objPos);
    32.        // localRotation = transform.localRotation;
    33.        CmdTransform(localPosition);
    34.    }
    35. //else we are not moving it or someone else is moving the object do this
    36.    else if(!receivedAuthority)
    37.    {
    38.        transform.localPosition = localPosition;
    39.        // transform.localRotation = localRotation;
    40.    }
    41. }
    42.  
    43. public override void OnStartAuthority()
    44. {
    45.    receivedAuthority = true;
    46. }
    47.  
    48. public override void OnStopAuthority()
    49. {
    50.    receivedAuthority = false;
    51. }
    52.  
    53. //tell all the clients what the updated position is
    54. [Command]
    55. public void CmdTransform(Vector3 position)
    56. {
    57.    if (!isLocalPlayer)
    58.    {
    59.      localPosition = position;
    60.      //localRotation = rotation;
    61.    }
    62. }
     
    jeffpw likes this.
  4. jeffpw

    jeffpw

    Joined:
    Aug 16, 2018
    Posts:
    1
    thanks for sharing. this was really helpful!