arcore,统一放置3D模型

时间:2019-05-13 07:25:45

标签: unity3d arcore

enter image description here

我正在使用ARCore和Unity制作AR应用程序。

我想在没有接触的情况下将3D模型放置在垂直墙上。

它几乎已实现,但是在创建3D模型时,它一直向左移。

我认为是因为设置了锚点位置

Vector3 position = new Vector3(0,0,0);

但是如果我将锚点位置更改为

 Vector3 position = new Vector3 (hit.Pose.position.x, hit.Pose.position.y, hit.Pose.position.z)

它也不到中心。

有人知道如何将3D对象放置在飞机中间吗?

TrackableHit hit;     
TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
TrackableHitFlags.FeaturePointWithSurfaceNormal;


if (Frame.Raycast(transform.position.x,transform.position.y, raycastFilter, out hit))
{
    if (CurrentNumberOfGameObjects < numberOfGameObjectsAllowed)
        {
            Destroy(ARObject);

        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
        {
            Vector3 position = new Vector3(0,0,0);
            Quaternion rotation = new Quaternion(0f, 0f, 0f, 1f);

            ARObject = Instantiate(ARAndroidPrefab, hit.Pose.position, hit.Pose.rotation);                                                  
            ARObject.transform.Rotate(-90, 0, 0, Space.Self);
            var anchor = Session.CreateAnchor(new Pose(position, rotation));
            ARObject.transform.parent = anchor.transform;
                CurrentNumberOfGameObjects = CurrentNumberOfGameObjects + 1;

                // Hide Plane once ARObject is Instantiated 
                foreach (GameObject Temp in DetectedPlaneGenerator.instance.PLANES) //RK
                {
                    Temp.SetActive(false);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我更改了代码

Vector3 position = Vector3.zero; // Vector3 position = new Vector3(0,0,0);

Vector3 position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y , hit.Pose.position.z);

然后它起作用。