Пример #1
0
    public void AttachToAnchor(TrackableHit hit)
    {
        // if moving off plane
        if (hit.Trackable == null)
        {
            Debug.Log("Card off plane!");
            return;
        }

        // move to hit position
        transform.position = hit.Pose.position;

        // if moving to new plane
        if (hit.Trackable != detectedPlane)
        {
            Debug.Log("Moving Card to new plane!");
            detectedPlane = (DetectedPlane)hit.Trackable;
            return;
        }

        // if does not have an anchor or too far away from current anchor
        // give it an anchor
        if (anchor == null || (anchor.transform.position - transform.position).magnitude > 1)
        {
            // gets all existing anchors on this plane
            List <Anchor> existingAnchors = new List <Anchor>();
            detectedPlane.GetAllAnchors(existingAnchors);

            bool anchorFound = false;
            // look for a nearby existing anchor
            foreach (Anchor a in existingAnchors)
            {
                // if found a nearby anchor
                if ((a.transform.position - hit.Pose.position).magnitude < 1)
                {
                    Debug.Log("Moving card to existing anchor!");
                    anchor      = a;
                    anchorFound = true;
                    break;
                }
            }

            //if did not find an anchor, create a new one
            if (!anchorFound)
            {
                Debug.Log("Creating new anchor!");
                anchor = detectedPlane.CreateAnchor(hit.Pose);
            }

            transform.SetParent(anchor.transform);
        }

        //Debug.Log("Anchor Position: " + anchor.transform.position);
        //Debug.Log("Distance From Anchor: " + transform.localPosition.magnitude);
    }