예제 #1
0
    public Vector2[] RefinePath(GameObject go, Vector2[] path, LayerMask unwalkableLayer)
    {
        if (path == null || path.Length <= 0)
        {
            return(path);
        }

        List <Vector2> refinedPath = new List <Vector2>(path);
        Vector2        colliderPosition;

        int i = 1;

        while (i < refinedPath.Count)
        {
            Vector2 startPos  = refinedPath[i - 1];
            Vector2 endPos    = refinedPath[i];
            Vector2 direction = DirectionAndDistanceCalculator.CalculateSignedDirection(startPos, endPos);
            if (direction.x != 0 && direction.y != 0)
            {
                if (ObstacleFinder.Instance.CheckObstacles(go, startPos, endPos, unwalkableLayer, out colliderPosition))
                {
                    Vector2 middle = DirectionAndDistanceCalculator.GetMiddlePosOfVector(startPos, endPos);
                    Vector2 newPoint;
                    if (colliderPosition.IsAbove(middle))
                    {
                        if (direction.y > 0)
                        {
                            newPoint = new Vector2(endPos.x, startPos.y);
                        }
                        else
                        {
                            newPoint = new Vector2(startPos.x, endPos.y);
                        }
                    }
                    else
                    {
                        if (direction.y > 0)
                        {
                            newPoint = new Vector2(startPos.x, endPos.y);
                        }
                        else
                        {
                            newPoint = new Vector2(endPos.x, startPos.y);
                        }
                    }
                    refinedPath.Insert(i, newPoint);
                }
            }
            i++;
        }
        return(refinedPath.ToArray());
    }
 public Vector2 GetMovementDirection()
 {
     return(DirectionAndDistanceCalculator.CalculateSignedDirection(gameObject.transform.Get2DPosition(), CurrentWayPoint));
 }