private void CreateWaypoints(Vector3[] corners)
        {
            List <Vector3> points = new List <Vector3>();

            for (int i = 0; i < corners.Length - 1; i++)
            {
                Vector3 start    = corners[i];
                Vector3 end      = corners[i + 1];
                float   division = Mathf.Floor((end - start).magnitude / m_WaypointStepSize);

                for (int j = 1; j < division; j++)
                {
                    float   blend = j / (division);
                    Vector3 p     = Vector3.Lerp(start, end, blend);
                    points.Add(p);
                }
            }

            if (points.Count > 1)
            {
                for (int i = 0; i < points.Count; i++)
                {
                    Vector3 direction;
                    if (i == 0)
                    {
                        direction = UnityToARSpace(m_ARSpace.transform, points[1]) - UnityToARSpace(m_ARSpace.transform, points[0]);
                    }
                    else
                    {
                        direction = UnityToARSpace(m_ARSpace.transform, points[i]) - UnityToARSpace(m_ARSpace.transform, points[i - 1]);
                    }

                    Vector3    pos = UnityToARSpace(m_ARSpace.transform, points[i]);
                    Quaternion rot = Quaternion.LookRotation(direction, m_ARSpace.transform.up);
                    GameObject go  = Instantiate(m_WaypointPrefab, pos, rot, transform);
                    go.transform.localScale = new Vector3(m_WaypointScale, m_WaypointScale, m_WaypointScale);
                    m_Waypoints.Add(go);
                }
            }

            // update compass m_Arrow direction
            if (m_Arrow != null && !m_Arrow.activeInHierarchy)
            {
                m_Arrow.SetActive(true);
            }

            if (corners.Length > 1 && m_ArrowDirection != null)
            {
                Vector3 cameraHeight = ARSpaceToUnity(m_ARSpace.transform, m_CameraTransform.position);
                Vector3 nextPoint    = new Vector3(corners[1].x, cameraHeight.y, corners[1].z);
                nextPoint = UnityToARSpace(m_ARSpace.transform, nextPoint);
                m_ArrowDirection.LookAt(nextPoint, m_ARSpace.transform.up);
            }
        }
Exemplo n.º 2
0
        public void UpdatePath(bool keepOldIfNotFound)
        {
            if (m_IsInitialized)
            {
                NavMeshPath path = new NavMeshPath();

                Vector3 startPosition  = ARSpaceToUnity(m_ARSpace.transform, m_ARSpace.initialOffset, m_CameraTransform.position);
                Vector3 targetPosition = ARSpaceToUnity(m_ARSpace.transform, m_ARSpace.initialOffset, m_TargetTransform.position);

                Vector3 currentPositionInUnitySpace = ARSpaceToUnity(m_ARSpace.transform, m_ARSpace.initialOffset, m_CameraTransform.position);
                Vector3 delta            = targetPosition - currentPositionInUnitySpace;
                float   distanceToTarget = new Vector3(delta.x, 0f, delta.z).magnitude;

                if (!m_NavigationArrived && distanceToTarget < navigationManager.arrivedDistanceThreshold)
                {
                    m_NavigationArrived = true;
                    navigationManager.DisplayArrivedNotification();
                    return;
                }

                if (NavMesh.CalculatePath(startPosition, targetPosition, NavMesh.AllAreas, path))
                {
                    if (!m_NavigationStarted)
                    {
                        navigationManager.DisplayNavigation();
                        m_NavigationStarted = true;
                        m_NavigationArrived = false;
                    }

                    m_IsNavigating = true;

                    List <Vector3> corners          = new List <Vector3>(path.corners);
                    List <Vector3> collapsedCorners = new List <Vector3>();

                    for (int i = 0; i < corners.Count; i++)
                    {
                        corners[i] = corners[i] + new Vector3(0f, m_yOffset, 0f);
                        corners[i] = UnityToARSpace(m_ARSpace.transform, m_ARSpace.initialOffset, corners[i]);
                    }

                    for (int i = 0; i < corners.Count - 1; i++)
                    {
                        Vector3 cp        = corners[i];
                        Vector3 np        = corners[i + 1];
                        float   threshold = 0.75f;

                        if (Vector3.Distance(cp, np) > threshold)
                        {
                            collapsedCorners.Add(cp);
                            //Debug.DrawLine(cp, cp + Vector3.up, Color.red);
                        }
                    }
                    collapsedCorners.Add(corners[corners.Count - 1]);

                    m_PathMeshGenerator.GeneratePath(collapsedCorners, m_ARSpace.transform.up);

                    // update compass m_Arrow direction
                    if (m_Arrow != null && !m_Arrow.activeInHierarchy)
                    {
                        m_Arrow.SetActive(true);
                    }

                    if (corners.Count > 1 && m_ArrowDirection != null)
                    {
                        Vector3 nextPoint = new Vector3(corners[1].x, Camera.main.transform.position.y, corners[1].z);
                        m_ArrowDirection.LookAt(nextPoint, Vector3.up);
                    }
                }
                else
                {
                    if (!keepOldIfNotFound)
                    {
                        navigationManager.DisplayPathNotFoundNotification();
                    }
                }
            }
        }