Skip to content

Instantly share code, notes, and snippets.

@andijakl
Created August 10, 2021 12:48
Show Gist options
  • Save andijakl/38bcd1effe29ea0cd5723bc1d289a92d to your computer and use it in GitHub Desktop.
Save andijakl/38bcd1effe29ea0cd5723bc1d289a92d to your computer and use it in GitHub Desktop.
Code part to attach an anchor to a plane in AR Foundation
ARAnchor CreateAnchor(in ARRaycastHit hit)
{
ARAnchor anchor;
// ... here, we'll place the plane anchoring code!
// If we hit a plane, try to "attach" the anchor to the plane
if (hit.trackable is ARPlane plane)
{
var planeManager = GetComponent<ARPlaneManager>();
if (planeManager)
{
var oldPrefab = _anchorManager.anchorPrefab;
_anchorManager.anchorPrefab = _prefabToPlace;
anchor = _anchorManager.AttachAnchor(plane, hit.pose);
_anchorManager.anchorPrefab = oldPrefab;
Debug.Log($"Created anchor attachment for plane (id: {anchor.nativePtr}).");
return anchor;
}
}
// Otherwise, just create a regular anchor at the hit pose
// (see previous code snippet)
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment