Загрузка данных


using UnityEngine;

public class RoutineManager : MonoBehaviour
{
    [SerializeField] Way[] paths;
    [SerializeField] Transform player;

    private Way currentPath;
    private Transform currentWayPoint;
    private Transform nextWaypoint;

    [SerializeField] float waypointReachedDistance = 3f;

    public Transform CurrentTarget => nextWaypoint;
    void Start()
    {
        if (paths.Length > 0)
        {
            SelectPath(0);
        }
    }

    void Update()
    {
        if (currentPath == null || nextWaypoint == null) return;

        UpdateNavigation();
    }

    void UpdateNavigation()
    {
        float distanceToWaypoint = Vector3.Distance(player.position, currentWayPoint.position);

        if (distanceToWaypoint < waypointReachedDistance )
        {
            if ( nextWaypoint != null )
            {
                currentWayPoint = nextWaypoint;
                nextWaypoint = currentPath.GetNextpoint(currentWayPoint);
                if (nextWaypoint == null )
                {
                    Debug.Log("arrived");
                }
            }
        }
        else
        {
            nextWaypoint = currentPath.GetNextpoint(currentWayPoint);
            if (nextWaypoint == null )
            {
                nextWaypoint = currentWayPoint;
            }
        }
    }

    public void SelectPath(int pathIndex)
    {
        if (pathIndex < paths.Length)
        {
            currentPath = paths[pathIndex];
            currentWayPoint = currentPath.GetNearestPoint(player.position);
            nextWaypoint = currentPath.GetNextpoint(currentWayPoint);
            if (nextWaypoint == null)
            {
                nextWaypoint = currentWayPoint;
            }
        }
    }
}