예제 #1
0
    // check for input action (phone touch)
    private void CheckForInputAction()
    {
        if (Input.touchCount > 0)
        {
            Touch touch        = Input.GetTouch(0);
            bool  bInputAction = true;

            switch (touch.phase)
            {
            case TouchPhase.Began:
                inputAction         = MultiARInterop.InputAction.Click;
                inputNavCoordinates = Vector3.zero;
                startInputPos       = touch.position;
                startTimestamp      = lastFrameTimestamp;
                break;

            case TouchPhase.Moved:
            case TouchPhase.Stationary:
                if ((lastFrameTimestamp - startTimestamp) >= 0.25)
                {
                    inputAction = MultiARInterop.InputAction.Grip;

                    float   screenMinDim = Screen.width < Screen.height ? Screen.width : Screen.height;
                    Vector3 mouseRelPos  = touch.position - startInputPos;
                    inputNavCoordinates = mouseRelPos / screenMinDim;
                }
                break;

            case TouchPhase.Ended:
                inputAction = MultiARInterop.InputAction.Release;
                break;

            default:
                bInputAction = false;
                break;
            }

            if (bInputAction)
            {
                inputPos       = touch.position;
                inputTimestamp = lastFrameTimestamp;
            }
        }
        else
        {
#if UNITY_EDITOR
            bool bInputAction = true;

            if (Input.GetMouseButtonDown(0))
            {
                inputAction    = MultiARInterop.InputAction.Click;
                startInputPos  = Input.mousePosition;
                startTimestamp = lastFrameTimestamp;
            }
            else if (Input.GetMouseButton(0))
            {
                if ((lastFrameTimestamp - startTimestamp) >= 0.25)
                {
                    inputAction = MultiARInterop.InputAction.Grip;

                    //Vector3 screenSize = new Vector3(Screen.width, Screen.height, 0f);
                    float   screenMinDim = Screen.width < Screen.height ? Screen.width : Screen.height;
                    Vector3 mouseRelPos  = Input.mousePosition - (Vector3)startInputPos;
                    inputNavCoordinates = mouseRelPos / screenMinDim;                      // new Vector3(mouseRelPos.x / screenSize.x, mouseRelPos.y / screenSize.y, 0f);
                }
            }
            else if (Input.GetMouseButtonUp(0))
            {
                inputAction = MultiARInterop.InputAction.Release;
            }
            else
            {
                bInputAction = false;
            }

            if (bInputAction)
            {
                inputPos       = Input.mousePosition;
                inputTimestamp = lastFrameTimestamp;
            }
#endif
        }
    }
    // Update is called once per frame
    void Update()
    {
        UpdateApplicationLifecycle();


        Touch touch;

        if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
        {
            return;
        }


        TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.PlaneWithinBounds;


        if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out TrackableHit hit))
        {
            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 detected plane!");
            }
            else
            {
                var FoxObject = Instantiate(arrowGameObject, hit.Pose.position, hit.Pose.rotation);
                FoxObject.transform.Rotate(0, mModelRotation, 0, Space.Self);
                var anchor = hit.Trackable.CreateAnchor(hit.Pose);
                FoxObject.transform.parent = anchor.transform;
            }
        }
        if (Input.GetMouseButtonDown(0) && followPathBool == 0)
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);


            if (Physics.Raycast(ray, out RaycastHit hitInfo))
            {
                var hits = new List <TrackableHit>();
                hitArr[i] = hits[i].Pose.position;
                i++;

                //var go = GameObject.Instantiate(arrowGameObject, hitInfo.point, Quaternion.identity);
                var go = GameObject.Instantiate(arrowGameObject, hit.Pose.position, Quaternion.identity);
                go.GetComponent <MeshRenderer>().material.color = UnityEngine.Random.ColorHSV();

                gameArray[i] = go;
                i++;
            }
        }
        //  Debug.Log(hit.Pose.position);


        if (followPathBool == 1)
        {
            float step = speed * Time.deltaTime;
            //hitArr[k] = hit.Pose.position;

            if (k < len)
            {
                travellingSalesman.transform.position = Vector3.MoveTowards(travellingSalesman.transform.position, hitArr[k], step);
                // travellingSalesman.transform.position += transform.forward * step;
                travellingSalesman.transform.LookAt(hitArr[k]);
                Vector3 relativePos = hitArr[k] - travellingSalesman.transform.position;

                Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
                travellingSalesman.transform.rotation = rotation;


                if (travellingSalesman.transform.position == hitArr[k])
                {
                    k++;
                }
                // Debug.Log(hitArr[k].ToString());
            }

            if (k == len)
            {
                followPathBool = 0;
                Destroy(travellingSalesman); // delete the instance gaile

                Reset();

                rabitVisible = false; // reset visibility to initial state
            }
        }
    }