Я не думаю, что проблема только в коллайдерах, потому что сфера действительно движется по лабиринту нормально, пока не совершит «ошибку», а потом иногда движется по диагонали.
я хочу, чтобы сфера перемещалась по лабиринту без коллайдеры или любая физика для начала.
это скрипт, который находит путь:
Код: Выделить всё
using System.Collections.Generic;
using UnityEngine;
public class Pathfinding : MonoBehaviour
{
public static List FindPath(bool[,] maze, Vector2Int start, Vector2Int end)
{
// Create a queue for BFS
Queue queue = new Queue();
queue.Enqueue(start);
// Keep track of visited cells and their parent cells
Dictionary parents = new Dictionary();
parents[start] = start;
// Directions for movement (Up, Down, Left, Right)
Vector2Int[] directions = {
new Vector2Int(0, 1), // Up
new Vector2Int(0, -1), // Down
new Vector2Int(-1, 0), // Left
new Vector2Int(1, 0) // Right
};
while (queue.Count > 0)
{
Vector2Int current = queue.Dequeue();
// If we've reached the end point, construct the path
if (current == end)
{
return ReconstructPath(parents, start, end);
}
// Explore neighbors
foreach (Vector2Int dir in directions)
{
Vector2Int neighbor = current + dir;
// Check bounds and if the neighbor is walkable and unvisited
if (neighbor.x >= 0 && neighbor.x < maze.GetLength(0) &&
neighbor.y >= 0 && neighbor.y < maze.GetLength(1) &&
!maze[neighbor.x, neighbor.y] && !parents.ContainsKey(neighbor))
{
queue.Enqueue(neighbor);
parents[neighbor] = current; // Set the parent of the neighbor
}
}
}
// If no path is found, return an empty list
return new List();
}
private static List ReconstructPath(Dictionary parents, Vector2Int start, Vector2Int end)
{
List path = new List();
Vector2Int current = end;
while (current != start)
{
path.Add(current);
current = parents[current];
}
path.Add(start); // Add the start point
path.Reverse(); // Reverse to get the path from start to end
return path;
}
}
Код: Выделить всё
IEnumerator MoveStartSphere()
{
// Get the path from start to end
List path = Pathfinding.FindPath(maze, startPoint, endPoint);
Vector2Int current = startPoint;
// Memory of visited positions to avoid looping
HashSet visited = new HashSet();
visited.Add(current);
while (current != endPoint)
{
// Calculate next step based on the path
Vector2Int nextStep = path[1]; // The next step in the shortest path
path.RemoveAt(0); // Remove the current step from the path
// Decide whether to make a mistake
if (allowMistakes && Random.value < 0.8f) // 80% chance to make a mistake
{
// Try to find a valid random neighbor
Vector2Int[] directions = {
new Vector2Int(0, 1), // Up
new Vector2Int(0, -1), // Down
new Vector2Int(-1, 0), // Left
new Vector2Int(1, 0) // Right
};
foreach (Vector2Int dir in directions)
{
Vector2Int randomNeighbor = current + dir;
}
}
// Debugging: Log the movement
Debug.Log($"Moving from {current} to {nextStep}");
// Move to the next step
Vector3 targetPosition = new Vector3(nextStep.x, 0.5f, nextStep.y);
while (Vector3.Distance(startSphere.transform.position, targetPosition) > 0.01f)
{
startSphere.transform.position = Vector3.MoveTowards(startSphere.transform.position, targetPosition, moveSpeed * Time.deltaTime);
yield return null;
}
// Update current position and mark it as visited
current = nextStep;
visited.Add(current);
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... -in-a-maze