Bad documentation for Anchor placement

Include the following details while filing a bug report (edit as applicable):

  • Issue category: ARDK Documentation
  • Device type & OS version: Any
  • Host machine & OS version: Any
  • Issue Environment : On Device
  • Xcode version:
  • ARDK version: 2.4.2
  • Unity version: 2021.3

Bug reproduction steps:
In the anchor documentation - Tracking AR Anchors — Niantic Lightship Augmented Reality Developer Kit release-2.4.2 documentation

The example given is

void SubscribeToSession(IARSession arSession)
{
  arSession.AnchorsUpdated += OnAnchorsUpdated;
}

void OnAnchorsUpdated(AnchorsArgs args)
{
  foreach (var anchor in args.Anchors)
  {
    // Find GameObject from Dictionary containing
    // existing Anchor to GameObject mappings
    var go = virtualObjects[anchor.Identifier];

    go.transform.position = anchor.Transform.ToPosition();
    go.transform.rotation = anchor.Transform.ToRotation();
  }
}

However, this is very bad as it will quickly fill up with exceptions, especially IF either Plane or Depth Finding is added, as they also create anchors, instead, the example should be as follows:

void SubscribeToSession(IARSession arSession)
{
  arSession.AnchorsUpdated += OnAnchorsUpdated;
}

void OnAnchorsUpdated(AnchorsArgs args)
{
    foreach (var anchor in args.Anchors)
    {
        if (instantiatedPrefabs.TryGetValue(anchor.Identifier, out var go))
        {
            go.transform.SetPositionAndRotation(anchor.Transform.ToPosition(), anchor.Transform.ToRotation());
        }
    }
}

To be safer and cleaner to execute (as well as using the Unity recommended “SetPositionAndRotation” function.

Hi Mr Jackson, I’ve put in a request to review the sample code for this feature and make it cleaner where possible. Thank you for looking out. Your input is making Lighthsip more usable for everyone

1 Like