Ejemplo n.º 1
0
 private void UpdateWaypointAngle()
 {
     if (_path.Count >= 2)
     {
         Vector3 nodePos = PathfinderData.Position(_path[_path.Count - 1]);
         Vector3 nextPos = PathfinderData.Position(_path[_path.Count - 2]);
         WaypointAngleChange = Math.Abs(Vector3.Angle(nodePos - _unit.transform.position, nextPos - nodePos));
     }
     else
     {
         WaypointAngleChange = 180;
     }
 }
Ejemplo n.º 2
0
 public Vector3 GetDestination()
 {
     return(_path.Count > 0 ? PathfinderData.Position(_path[0]) : NO_POSITION);
 }
Ejemplo n.º 3
0
        private void UpdateWaypoint()
        {
            PathNode targetNode = _path[_path.Count - 1];

            float distance = Vector3.Distance(
                _unit.transform.position, PathfinderData.Position(targetNode));

            if (distance < (_path.Count > 1 ? COMPLETION_DIST : FinalCompletionDist))
            { // Unit arrived at the next path node
                _path.RemoveAt(_path.Count - 1);
                if (!HasDestination())
                { // Unit arrived at the destination
                    _waypoint = NO_POSITION;
                    return;
                }
                else
                {
                    _previousNode = targetNode;
                    targetNode    = _path[_path.Count - 1];
                    UpdateWaypointAngle();
                }
            }

            Vector3 targetPosition = PathfinderData.Position(targetNode);

            // If the unit is supposed to be moving along a road, this will push it more toward the center
            // to avoid having it drive along next to the road.
            if (targetNode.IsRoad && _previousNode != null && _previousNode.IsRoad)
            {
                targetPosition = GetRoadIntersection(
                    targetPosition,
                    PathfinderData.Position(_previousNode),
                    _unit.transform.position);
            }

            Vector3 newWaypoint = TakeStep(
                Data,
                _unit.transform.position,
                targetPosition,
                _unit.Mobility,
                _unit.Data.Radius);

            if (newWaypoint != NO_POSITION)
            {
                _waypoint = _s_straightStep ? targetPosition : newWaypoint;
            }
            else
            {
                // The unit has gotten stuck when following the previously computed path.
                // Now recompute a new path to the destination using the global graph, this time using finite radius
                float pathTime = Data.FindPath(
                    _path,
                    _unit.transform.position,
                    PathfinderData.Position(_path[0]),
                    _unit.Mobility,
                    _unit.Data.Radius,
                    Command);
                //float pathTime = SetPath (path[0].position, command);

                bool isTrapped = pathTime == FOREVER;
                if (isTrapped)
                {
                    Debug.Log("I am stuck!!!");
                    _waypoint = NO_POSITION;
                }
                else
                {
                    // If this is an intermediate step of the path, then the pre-computed global graph might
                    // be broken and the corresponding arc should be recomputed to avoid having units cycle forever
                    if (_previousNode != null && _path.Count > 1)
                    {
                        Data.RemoveArc(_previousNode, targetNode);
                        Data.AddArc(_previousNode, targetNode);
                    }
                }
            }
        }