Coordinate systems in Unity

Windows Mixed Reality supports apps across a wide range of experience scales, from orientation-only and seated-scale apps up through room-scale apps. On HoloLens, you can go further and build world-scale apps that let users walk beyond 5 meters, exploring an entire floor of a building and beyond.

Your first step in building a mixed reality experience in Unity is to determine which experience scale your app will target.

Building an orientation-only or seated-scale experience

Namespace: UnityEngine.VR
Type: InputTracking

To build an orientation-only or seated-scale experience, you can place content directly in Unity's world coordinate system, which defaults to the stationary frame of reference. Content placed in the editor just in front of the camera's default location (forward is -Z) will appear in front of the user when the app launches.

For a pure orientation-only experience such as a 360-degree video viewer (where positional head updates would ruin the illusion), you can set VR.InputTracking.disablePositionalTracking to true:

InputTracking.disablePositionalTracking = true;

For a seated-scale experience, to let the user later recenter the seated origin, you can call the VR.InputTracking.Recenter method:

InputTracking.Recenter();

Building a standing-scale or room-scale experience

Namespace: UnityEngine.VR.WSA
Type: StageRoot

For a standing-scale or room-scale experience, you'll need to place content relative to the floor. You reason about the user's floor using the spatial stage, which represents the user's defined floor-level origin and optional room boundary, set up during first run:

  1. To anchor a branch of your Unity hierarchy to the user's defined floor origin, add the StageRoot component to a root game object. The position and orientation of this object will be adjusted each frame to remain precisely in place at the user-defined location on the floor where the user intends to start Mixed Reality experiences, facing their defined forward direction (-Z). Child objects placed at Y=0 will then appear to be on the floor.
  2. In script code, you can then call the TryGetBounds methods on your StageRoot to get a boundary polygon. If the user defined a boundary, you can build a room-scale experience. You can test the placement of your content against this boundary polygon to ensure the user can physically reach those objects. Note that the system will automatically render the boundary when the user approaches it. Your app does not need to render the boundary itself.

Building a world-scale experience

Namespace: UnityEngine.VR.WSA
Type: WorldAnchor

For true world-scale experiences on HoloLens that let users wander beyond 5 meters, you'll need new techniques beyond those used for room-scale experiences. One key technique you'll use is to create a spatial anchor to lock a cluster of holograms precisely in place in the physical world, regardless of how far the user has roamed, and then find those holograms again in later sessions.

In Unity, you create a spatial anchor by adding the WorldAnchor Unity component to a GameObject.

Adding a World Anchor

To add a world anchor, call AddComponent<WorldAnchor>() on the game object with the transform you want to anchor in the real world.

WorldAnchor anchor = gameObject.AddComponent<WorldAnchor>();

That's it! This game object will now be anchored to its current location in the physical world - you may see its Unity world coordinates adjust slightly over time to ensure that physical alignment. Use persistence to find this anchored location again in a future app session.

Removing a World Anchor

If you no longer want the GameObject locked to a physical world location and don't intend on moving it this frame, then you can just call Destroy on the World Anchor component.

Destroy(gameObject.GetComponent<WorldAnchor>());

If you want to move the GameObject this frame, you need to call DestroyImmediate instead.

DestroyImmediate(gameObject.GetComponent<WorldAnchor>());

Moving a World Anchored GameObject

GameObject's cannot be moved while a World Anchor is on it. If you need to move the GameObject this frame, you need to:

  1. DestroyImmedaite the World Anchor component
  2. Move the GameObject
  3. Add a new World Anchor component to the GameObject.
DestroyImmediate(gameObject.GetComponent<WorldAnchor>());
gameObject.transform.position = new Vector3(0, 0, 2);
WorldAnchor anchor = gameObject.AddComponent<WorldAnchor>();

Handling Locatability Changes

A WorldAnchor may not be locatable in the physical world at a point in time. If that occurs, Unity will not be updating the transform of the anchored object. This also can change while an app is running. Failure to handle the change in locatability will cause the object to not appear in the correct physical location in the world.

To be notified about locatability changes:

  1. Subscribe to the OnTrackingChanged event
  2. Handle the event

The OnTrackingChanged event will be called whenever the underlying spatial anchor changes between a state of being locatable vs. not being locatable.

anchor.OnTrackingChanged += Anchor_OnTrackingChanged;

Then handle the event:

private void Anchor_OnTrackingChanged(WorldAnchor self, bool located)
{
       // This simply activates/deactivates this object and all children when tracking changes
    self.gameObject.SetActiveRecursively(located);
}

Sometimes anchors are located immediately. In this case, this isLocated property of the anchor will be set to true when AddComponent<WorldAnchor>() returns. As a result, the OnTrackingChanged event will not be triggered. A clean pattern would be to call your OnTrackingChanged handler with the initial IsLocated state after attaching an anchor.

Anchor_OnTrackingChanged(anchor, anchor.isLocated);

See Also