示例#1
0
    private void Update()
    {
        float distance = Vector3.Distance(transform.position, path[cellIndex].transform.position);

        if (distance < 0.1f)
        {
            cellIndex++;
            if (cellIndex == path.Count)
            {
                Vector2Int gridSize = grid.GetGridSize();
                do
                {
                    path = PathFinder.FindPath(grid.GetGrid(), grid.GetGridXY(transform.position), new Vector2Int(Random.Range(0, gridSize.x), Random.Range(0, gridSize.y)), diag);
                }while (path == null);
                cellIndex = 0;
            }
        }

        transform.position = Vector3.MoveTowards(transform.position, path[cellIndex].transform.position, speed * Time.deltaTime);
    }