Код: Выделить всё
if (x == 1 && y == 0)
{
wall.GetComponent().material = startMaterial;
}
if (x == width - 1 && y == height - 1)
{
wall.GetComponent().material = endMaterial;
}
Код: Выделить всё
wall.GetComponent().material = startMaterial;
Код: Выделить всё
if (x == width - 1 && y == height - 1)
как можно я это сделаю?
вот скриншот того, что сейчас происходит:
лабиринт
я думаю желтый цвет находится в правильном месте в качестве начальной точки, а черный - в правильном месте в качестве конечной точки. я также думаю, что это неправильный способ определения начальной и конечной точек.
вот полный сценарий:
Код: Выделить всё
using UnityEngine;
public class MazeGenerator : MonoBehaviour
{
public int width = 10; // Width of the maze
public int height = 10; // Height of the maze
public GameObject wallPrefab; // Wall prefab
public GameObject floorPrefab; // Floor prefab
public bool includeExit = true; // Option for exit
public bool includeStartAndEndPoints = true; // Option for start and end points
public Material startMaterial;
public Material endMaterial;
private bool[,] maze; // Maze grid (true = wall, false = path)
void Start()
{
GenerateMaze();
BuildMaze();
}
void GenerateMaze()
{
// Initialize the maze with walls
maze = new bool[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
maze[x, y] = true;
}
}
// Start carving paths
CarvePath(1, 1);
}
void CarvePath(int x, int y)
{
maze[x, y] = false; // Set path
int[] directions = { 0, 1, 2, 3 }; // Up, Down, Left, Right
ShuffleArray(directions); // Shuffle directions
foreach (int dir in directions)
{
int nx = x, ny = y;
switch (dir)
{
case 0: ny -= 2; break; // Up
case 1: ny += 2; break; // Down
case 2: nx -= 2; break; // Left
case 3: nx += 2; break; // Right
}
if (nx > 0 && nx < width - 1 && ny > 0 && ny < height - 1 && maze[nx, ny])
{
maze[(x + nx) / 2, (y + ny) / 2] = false; // Remove wall between
CarvePath(nx, ny);
}
}
}
void BuildMaze()
{
// Clear the old maze in case of regeneration
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
// Generate maze
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector3 position = new Vector3(x, 0, y);
if (maze[x, y])
{
// Instantiate wall prefab
GameObject wall = Instantiate(wallPrefab, position, Quaternion.identity, transform);
if (x == 1 && y == 0)
{
wall.GetComponent().material = startMaterial;
}
if (x == width - 1 && y == height - 1)
{
wall.GetComponent().material = endMaterial;
}
wall.transform.localScale = new Vector3(1, 2, 1); // Adjust wall height and width
}
else
{
// Instantiate floor prefab
Instantiate(floorPrefab, position, Quaternion.identity, transform);
}
}
}
if (includeExit)
{
CreateExit();
}
}
void CreateExit()
{
int exitX = width - 2; // Set exit near the end
int exitY = height - 2; // Set exit near the end
maze[exitX, exitY] = false;
Vector3 exitPosition = new Vector3(exitX, 0, exitY);
Instantiate(floorPrefab, exitPosition, Quaternion.identity, transform);
}
void ShuffleArray(int[] array)
{
for (int i = array.Length - 1; i > 0; i--)
{
int randomIndex = Random.Range(0, i + 1);
int temp = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
}
}
Код: Выделить всё
if (x == 1 && y == 0)
{
wall.GetComponent().material = startMaterial;
}
if (x == width - 1 && y == height - 1)
{
wall.GetComponent().material = endMaterial;
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... ts-objects
Мобильная версия