如何在由ARCore生成的平面上沿摄像机视图的方向移动对象/预制件

时间:2019-02-16 10:26:16

标签: unity3d arcore

我正在使用LeanTouch +转换对象,但是它只能基于android屏幕在Y轴上移动,因此在相机​​上只能上下移动对象。我要实现的目标是能够将对象进一步移近或移近相机视图。在ARCore中,您首先检测到环境,然后将其生成为Plane,这就是我需要对象移动到的平面。

如果可能的话,我也想在“检测到的平面”上使用“夹钳”来限制运动,使其超出检测到的平面

这是我使用LeanFingerTap生成obj的代码

public void Spawn(LeanFinger finger)
    {
        if (AndyPlanePrefab != null && finger != null)
        {
            // Raycast against the location the player touched to search for planes.
            TrackableHit hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                TrackableHitFlags.FeaturePointWithSurfaceNormal;

            if (Frame.Raycast(finger.ScreenPosition.x, finger.ScreenPosition.y, raycastFilter, out hit))
            {
                if(currentNumberofPrefab<numberOfPrefabsAllowed)
                {
                    currentNumberofPrefab = currentNumberofPrefab + 1;

                    // Use hit pose and camera pose to check if hittest is from the
                    // back of the plane, if it is, no need to create the anchor.
                    if ((hit.Trackable is DetectedPlane) &&
                        Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                            hit.Pose.rotation * Vector3.up) < 0)
                    {
                        Debug.Log("Hit at back of the current DetectedPlane");
                    }
                    else
                    {
                        // Choose the Andy model for the Trackable that got hit.
                        if (hit.Trackable is FeaturePoint)
                        {
                            AndyPrefab = AndyPointPrefab;
                        }
                        else
                        {
                            getValue = ItemScrollList.valuePrefab;
                            if (getValue == 1)
                            {
                                AndyPrefab = AndyPlanePrefab[0];
                                Debug.Log("value 1");
                            }
                            else if (getValue == 2)
                            {
                                AndyPrefab = AndyPlanePrefab[1];
                                Debug.Log("value 2");
                            }
                            else if (getValue == 3)
                            {
                                AndyPrefab = AndyPlanePrefab[2];
                                Debug.Log("value 3");
                            }
                            else if (getValue == 4)
                            {
                                AndyPrefab = AndyPlanePrefab[3];
                                Debug.Log("value 4");
                            }
                            else if (getValue == 5)
                            {
                                AndyPrefab = AndyPlanePrefab[4];
                                Debug.Log("value 5");
                            }
                        }

                        // Instantiate Andy model at the hit pose.
                        var andyObject = Instantiate(AndyPrefab, hit.Pose.position, hit.Pose.rotation);
                        //adding tag to andyObject for destroying spawn purposes
                        andyObject.tag = "HomePrefab";

                        // Compensate for the hitPose rotation facing away from the raycast (i.e. camera).
                        andyObject.transform.Rotate(0, k_ModelRotation, 0, Space.Self);

                        // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
                        // world evolves.
                        var anchor = hit.Trackable.CreateAnchor(hit.Pose);

                        // Make Andy model a child of the anchor.
                        andyObject.transform.parent = anchor.transform;
                    }
                }

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以通过在(0, 0, 0)中创建一个预制的游戏对象来实现此目的,该预制对象具有空的游戏对象作为父对象,而您的对象作为子对象。

然后,当您检测到飞机时,将使用Session.GetTrackable进入飞机列表。 然后,您可以使用以下代码在检测到的平面中心实例化您的预制件:

Anchor anchor = m_AllPlanes[0].CreateAnchor(m_AllPlanes[0].CenterPose);
var obj = Instantiate(myPrefab, m_AllPlanes[0].CenterPose);
obj.transform.parent = anchor.transform;

然后,如果您希望将obj移得更远或更靠近相机,则可以将其移动到本地z中。然后,您的钳位可以为m_AllPlanes[0].CenterPose.position.z - m_AllPlanes[0].ExtentZm_AllPlanes[0].CenterPose.position.z + m_AllPlanes[0].ExtentZ

我之所以建议使用游戏对象创建预制件,是因为ARCore坐标在每个Session中都会发生变化,因此能够使用本地坐标。