gpt-4o-2024-05-13· OpenAI
WildChat-4.8M (AI2)
привет напиши прикольные но сложный скрипт на юнити 3д
- User
привет напиши прикольные но сложный скрипт на юнити 3д
- Assistant
привет напиши прикольные но сложный скрипт на юнити 3д
Конечно, я постараюсь сделать что-то интересное для вас! Как насчет того, чтобы создать скрипт для генерации случайного лабиринта в 3D-пространстве? Это достаточно сложная, но увлекательная задача. Мы будем использовать алгоритм "Depth-First Search" (поиск в глубину) для генерации лабиринта.
Вот пример скрипта на C# для Unity:
MazeGenerator, прикрепите его к объекту "MazeGenerator".Вот код для скрипта MazeGenerator.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator : MonoBehaviour
{
public int width = 10;
public int height = 10;
public GameObject wallPrefab;
private int[,] maze;
private void Start()
{
maze = new int[width, height];
GenerateMaze();
DrawMaze();
}
private void GenerateMaze()
{
Stack<Vector2Int> stack = new Stack<Vector2Int>();
Vector2Int currentCell = new Vector2Int(0, 0);
maze[currentCell.x, currentCell.y] = 1;
while (true)
{
List<Vector2Int> neighbors = GetUnvisitedNeighbors(currentCell);
if (neighbors.Count > 0)
{
Vector2Int chosenNeighbor = neighbors[Random.Range(0, neighbors.Count)];
stack.Push(currentCell);
RemoveWall(currentCell, chosenNeighbor);
currentCell = chosenNeighbor;
maze[currentCell.x, currentCell.y] = 1;
}
else if (stack.Count > 0)
{
currentCell = stack.Pop();
}
else
{
break;
}
}
}
private List<Vector2Int> GetUnvisitedNeighbors(Vector2Int cell)
{
List<Vector2Int> neighbors = new List<Vector2Int>();
Vector2Int[] directions = { Vector2Int.up, Vector2Int.down, Vector2Int.left, Vector2Int.right };
foreach (Vector2Int direction in directions)
{
Vector2Int neighbor = cell + direction;
if (IsInsideMaze(neighbor) && maze[neighbor.x, neighbor.y] == 0)
{
neighbors.Add(neighbor);
}
}
return neighbors;
}
private bool IsInsideMaze(Vector2Int position)
{
return position.x >= 0 && position.x < width && position.y >= 0 && position.y < height;
}
private void RemoveWall(Vector2Int currentCell, Vector2Int chosenNeighbor)
{
Vector2Int wallPosition = (currentCell + chosenNeighbor) / 2;
maze[wallPosition.x, wallPosition.y] = 1;
}
private void DrawMaze()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if (maze[x, y] == 0)
{
Instantiate(wallPrefab, new Vector3(x * 2, 0, y * 2), Quaternion.identity);
}
}
}
}
}
Этот скрипт создаёт лабиринт размером width на height с использованием алгоритма поиска в глубину. Стены будут представлять собой префабы (prefabs), которые необходимо создать заранее.
wallPrefab.Надеюсь, вам понравится этот проект!
Not yet included in a published release.