Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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