Esempio n. 1
0
    private IEnumerator PlaceCarrotCoroutine()
    {
        isPlacingCarrot = true;
        Ray               cameraRay;
        TrackableHit      hit;
        TrackableHitFlags filter          = TrackableHitFlags.PlaneWithinPolygon;
        DetectedPlane     planeToPlace    = null;
        Vector3           positionToPlace = Vector3.zero;
        GameObject        potMock         = null;

        while (isPlacingCarrot)
        {
            cameraRay = FirstPersonCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
            if (Frame.Raycast(cameraRay.origin, cameraRay.direction, out hit, float.PositiveInfinity, filter))
            {
                DetectedPlane plane;
                if (hit.Trackable is DetectedPlane)
                {
                    plane = (DetectedPlane)hit.Trackable;
                    if (!plane.IsVerticalPlane() &&
                        Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                                    hit.Pose.rotation * Vector3.up) > 0)
                    {
                        planeToPlace    = plane;
                        positionToPlace = hit.Pose.position;
                        if (potMock == null)
                        {
                            potMock = Instantiate(PotMockPrefab, positionToPlace, planeToPlace.CenterPose.rotation);
                        }
                        else
                        {
                            potMock.transform.position = positionToPlace;
                            potMock.transform.rotation = planeToPlace.CenterPose.rotation;
                        }

                        MenusController.EnableAcceptButton();
                    }
                }
                else
                {
                    MenusController.DisableAcceptButton();
                }
            }
            else
            {
                if (potMock != null)
                {
                    Destroy(potMock);
                }

                MenusController.DisableAcceptButton();
            }

            if (placingFinished)
            {
                OnPlacingCarrotExit();
                if (potMock != null)
                {
                    Destroy(potMock);
                }

                if (planeToPlace != null) // cant check Vector3 for null
                {
                    var potCarrot = Instantiate(PotPrefab, positionToPlace, planeToPlace.CenterPose.rotation);
                    potCarrot.GetComponent <PotCarrotController>().SetPlane(planeToPlace);
                    PotCarrots.Add(potCarrot.GetComponent <PotCarrotController>());
                }

                placingFinished = false;
                isPlacingCarrot = false;
                yield break;
            }

            yield return(null);
        }

        if (potMock != null)
        {
            Destroy(potMock);
        }

        OnPlacingCarrotExit();
    }