Beispiel #1
0
        float CalculatePathLength(Vector3 targetPosition)
        {
            // Create a path and set it based on a target position.
            UnityEngine.AI.NavMeshPath path = new UnityEngine.AI.NavMeshPath();
            if (nav.enabled)
            {
                nav.CalculatePath(targetPosition, path);
            }

            // Create an array of points which is the length of the number of corners in the path + 2.
            Vector3[] allWayPoints = new Vector3[path.corners.Length + 2];

            // The first point is the enemy's position.
            allWayPoints[0] = transform.position;

            // The last point is the target position.
            allWayPoints[allWayPoints.Length - 1] = targetPosition;

            // The points inbetween are the corners of the path.
            for (int i = 0; i < path.corners.Length; i++)
            {
                allWayPoints[i + 1] = path.corners[i];
            }

            // Create a float to store the path length that is by default 0.
            float pathLength = 0;

            // Increment the path length by an amount equal to the distance between each waypoint and the next.
            for (int i = 0; i < allWayPoints.Length - 1; i++)
            {
                pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]);
            }

            return(pathLength);
        }
        // -------------------------------------------------------------------------
        public override bool Update()
        // -------------------------------------------------------------------------
        {
            if (base.Update() || _done)
            {
                return(true);
            }

            Vector3 newPt = Vector3.zero;

            if (PT_Game.Match.Arena.FindTerrainPointUnderMouse(PT_Game.Match.MainCamera, ref newPt))
            {
                _prevTerrainPoint = _curTerrainPoint;
                _curTerrainPoint  = newPt;
            }
            else
            {
                _lineId = PT_Game.Match.Widgets.HidePath(_lineId);
                return(false);
            }

            if (Input.GetKey(KeyCode.Space))
            {
                Debug.Break();
            }

            if (Input.GetMouseButtonDown(0))        // ### TODO : Change to input mapping
            {
                _done = true;
                Events.SendGlobal(new MT_SelectedPathEvent()
                {
                    Where = _curTerrainPoint, Path = _pointList
                });
                return(true);
            }

            if ((_curTerrainPoint - _prevTerrainPoint).sqrMagnitude > _distBeforeRecalc)   // haven't moved far
            {
                _nma.CalculatePath(_curTerrainPoint, _path);
            }

            switch (_nma.pathStatus)
            {
            case UnityEngine.AI.NavMeshPathStatus.PathComplete:
                SetLine(_path);
                break;

            case UnityEngine.AI.NavMeshPathStatus.PathPartial:
                break;

            case UnityEngine.AI.NavMeshPathStatus.PathInvalid:
                break;
            }
            return(false);
        }
Beispiel #3
0
        private void CalcuteAndMove()
        {
            float h;
            float v;
            float r;
            bool  isInput = IsInputControl(out h, out v, out r);

            m_move = Vector3.zero;
            if (isInput)
            {
                if (m_isNavigate)
                {
                    InternalImmediateStop();
                }
                m_move = GetInputMovement(h, v, r);
            }
            else if (m_isNavigate)
            {
                m_agent.SetDestination(m_TargetPosition);
                if (m_agent.pathPending)
                {
                    // Debug.Log("m_agent.pathPending");
                }
                else if (m_agent.remainingDistance < m_agent.stoppingDistance)
                {
                    ImmediateStop();

                    if (onCalcuteNaviPoints != null)
                    {
                        onCalcuteNaviPoints.Invoke(null);
                    }
                }
                else
                {
                    if (m_agent.CalculatePath(m_TargetPosition, path))
                    {
                        if (onCalcuteNaviPoints != null)
                        {
                            onCalcuteNaviPoints.Invoke(path);
                        }
                    }
                    else
                    {
                        Debug.Log("can not calcute path:" + path);
                    }
                    m_move = m_agent.desiredVelocity;
                }
            }
            MovePlayer(m_move);
        }
Beispiel #4
0
        void GoToWaypoint()
        {
            UnityEngine.AI.NavMeshPath path = new UnityEngine.AI.NavMeshPath();
            Vector3 newLocation             = Vector3.zero;

            while (path.status == UnityEngine.AI.NavMeshPathStatus.PathPartial || path.status == UnityEngine.AI.NavMeshPathStatus.PathInvalid)
            {
                Vector3 ran = Random.insideUnitSphere * WanderRadius;
                ran.y       = _startingPos.y;
                newLocation = _startingPos + ran;
                _navMeshAgent.CalculatePath(newLocation, path);
            }
            _navMeshAgent.SetDestination(newLocation);

            _hasReachedDestination = false;
        }