我如何更改迷宫生成部分,使迷宫位于地形 X、Z 而不是 Y?
How can i change the maze generate part so the maze will be on the terrain X,Z not Y?
迷宫立在地形上:
我想创建它:
我自己将 X 上的 GameObject 旋转更改为 90
但是我是在比赛 运行 的时候做的。我希望它在创建迷宫时是这样的。
这是迷宫 class 所有设置和生成:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Maze
{
//Grid size
public int width;
public int height;
//Store grid
private bool[,] grid;
//Generate random directions to move
private System.Random rg;
//Start position
int startX;
int startY;
//Public getter
public bool[,] Grid
{
get { return grid; }
}
//Constructor of the grid for setting values
public Maze(int width, int height, System.Random rg)
{
this.width = width;
this.height = height;
this.rg = rg;
}
//Generate the grid
public void Generate()
{
grid = new bool[width, height];
startX = 1;
startY = 1;
grid[startX, startY] = true;
MazeDigger(startX, startY);
}
void MazeDigger(int x, int y)
{
int[] directions = new int[] { 1, 2, 3, 4 };
//We create random array of directions
HelpingTools.Shuffle(directions, rg);
//We are looping over all the directions
for (int i = 0; i < directions.Length; i++)
{
if (directions[i] == 1)
{
if (y - 2 <= 0)
continue;
if (grid[x, y - 2] == false)
{
grid[x, y - 2] = true;
grid[x, y - 1] = true;
MazeDigger(x, y - 2);
}
}
if (directions[i] == 2)
{
if (x - 2 <= 0)
continue;
if (grid[x - 2, y] == false)
{
grid[x - 2, y] = true;
grid[x - 1, y] = true;
MazeDigger(x - 2, y);
}
}
if (directions[i] == 3)
{
if (x + 2 >= width - 1)
continue;
if (grid[x + 2, y] == false)
{
grid[x + 2, y] = true;
grid[x + 1, y] = true;
MazeDigger(x + 2, y);
}
}
if (directions[i] == 4)
{
if (y + 2 >= height - 1)
continue;
if (grid[x, y + 2] == false)
{
grid[x, y + 2] = true;
grid[x, y + 1] = true;
MazeDigger(x, y + 2);
}
}
}
}
}
这是创建随机迷宫数组的class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelpingTools : MonoBehaviour
{
public static T[] Shuffle<T>(T[] array, System.Random rg)
{
for (int i = 0; i < array.Length - 1; i++)
{
int randomIndex = rg.Next(i, array.Length);
T tempItem = array[randomIndex];
array[randomIndex] = array[i];
array[i] = tempItem;
}
return array;
}
}
这就是我画迷宫的方式:
在 CreateMaze 方法中的 X 上实例化旋转 90 度时,我可以更改每个立方体,但我不确定这是否是一个好的解决方案:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator : MonoBehaviour
{
public Maze maze;
public int mazeWidth;
public int mazeHeight;
public string mazeSeed;
public GameObject wallPrefab;
private GameObject wall;
private GameObject wallCorner;
private System.Random mazeRG;
private GameObject[] bricks;
// Use this for initialization
void Start ()
{
mazeRG = new System.Random();
if (mazeWidth % 2 == 0)
mazeWidth++;
if (mazeHeight % 2 == 0)
{
mazeHeight++;
}
maze = new Maze(mazeWidth, mazeHeight, mazeRG);
GenerateMaze();
}
public void GenerateMaze()
{
bricks = GameObject.FindGameObjectsWithTag("MazeBrick");
if (bricks.Length > 0)
DestroyMaze();
maze.Generate();
DrawMaze();
}
private void DestroyMaze()
{
for(int i = 0; i < bricks.Length; i++)
{
DestroyImmediate(bricks[i]);
}
}
void DrawMaze()
{
for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
Vector3 position = new Vector3(x, y);
if (maze.Grid[x, y] == true)
{
CreateMaze(position, transform, 0, mazeRG.Next(0, 3) * 90);
}
}
}
}
void CreateMaze(Vector3 position, Transform parent, int sortingOrder, float rotation)
{
GameObject mazePrefab = Instantiate(wallPrefab, position, Quaternion.identity);
mazePrefab.transform.SetParent(parent);
mazePrefab.transform.Rotate(0, 0, rotation);
mazePrefab.tag = "MazeBrick";
}
// Update is called once per frame
void Update () {
}
}
最后一个检查器中按钮的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MazeGenerator))]
public class GenerateButton : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
MazeGenerator myScript = (MazeGenerator)target;
if (GUILayout.Button("Generate Maze"))
{
myScript.GenerateMaze();
}
}
}
虽然游戏是 运行 每次我单击检查器中的按钮时,它都会在 MazeGeneratore 脚本中生成一个新迷宫:
public void GenerateMaze()
{
bricks = GameObject.FindGameObjectsWithTag("MazeBrick");
if (bricks.Length > 0)
DestroyMaze();
maze.Generate();
DrawMaze();
}
但这会创建新的迷宫,重新站起来。
要一次旋转所有的立方体,我会旋转它们都附加到的父对象。我会简单地在您的 MazeGenerator
脚本的 Start
函数中更改父对象的旋转,因此您的 Start
将如下所示:
void Start ()
{
mazeRG = new System.Random();
if (mazeWidth % 2 == 0)
mazeWidth++;
if (mazeHeight % 2 == 0)
{
mazeHeight++;
}
maze = new Maze(mazeWidth, mazeHeight, mazeRG);
GenerateMaze();
transform.localRotation = Quaternion.Euler(90, 0, 0);
}
当然,您必须在生成迷宫后执行此操作,以确保立方体位置正确。这会将父项的局部旋转设置为沿 X 轴旋转 90 度。
希望能帮到你!
为了使其平整而不是旋转到位,请更改此行(在 MazeGenerator::DrawMaze 中):
Vector3 position = new Vector3(x, y);
收件人:
Vector3 position = new Vector3(x, 0, y);
迷宫立在地形上:
我想创建它:
我自己将 X 上的 GameObject 旋转更改为 90 但是我是在比赛 运行 的时候做的。我希望它在创建迷宫时是这样的。
这是迷宫 class 所有设置和生成:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Maze
{
//Grid size
public int width;
public int height;
//Store grid
private bool[,] grid;
//Generate random directions to move
private System.Random rg;
//Start position
int startX;
int startY;
//Public getter
public bool[,] Grid
{
get { return grid; }
}
//Constructor of the grid for setting values
public Maze(int width, int height, System.Random rg)
{
this.width = width;
this.height = height;
this.rg = rg;
}
//Generate the grid
public void Generate()
{
grid = new bool[width, height];
startX = 1;
startY = 1;
grid[startX, startY] = true;
MazeDigger(startX, startY);
}
void MazeDigger(int x, int y)
{
int[] directions = new int[] { 1, 2, 3, 4 };
//We create random array of directions
HelpingTools.Shuffle(directions, rg);
//We are looping over all the directions
for (int i = 0; i < directions.Length; i++)
{
if (directions[i] == 1)
{
if (y - 2 <= 0)
continue;
if (grid[x, y - 2] == false)
{
grid[x, y - 2] = true;
grid[x, y - 1] = true;
MazeDigger(x, y - 2);
}
}
if (directions[i] == 2)
{
if (x - 2 <= 0)
continue;
if (grid[x - 2, y] == false)
{
grid[x - 2, y] = true;
grid[x - 1, y] = true;
MazeDigger(x - 2, y);
}
}
if (directions[i] == 3)
{
if (x + 2 >= width - 1)
continue;
if (grid[x + 2, y] == false)
{
grid[x + 2, y] = true;
grid[x + 1, y] = true;
MazeDigger(x + 2, y);
}
}
if (directions[i] == 4)
{
if (y + 2 >= height - 1)
continue;
if (grid[x, y + 2] == false)
{
grid[x, y + 2] = true;
grid[x, y + 1] = true;
MazeDigger(x, y + 2);
}
}
}
}
}
这是创建随机迷宫数组的class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelpingTools : MonoBehaviour
{
public static T[] Shuffle<T>(T[] array, System.Random rg)
{
for (int i = 0; i < array.Length - 1; i++)
{
int randomIndex = rg.Next(i, array.Length);
T tempItem = array[randomIndex];
array[randomIndex] = array[i];
array[i] = tempItem;
}
return array;
}
}
这就是我画迷宫的方式: 在 CreateMaze 方法中的 X 上实例化旋转 90 度时,我可以更改每个立方体,但我不确定这是否是一个好的解决方案:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator : MonoBehaviour
{
public Maze maze;
public int mazeWidth;
public int mazeHeight;
public string mazeSeed;
public GameObject wallPrefab;
private GameObject wall;
private GameObject wallCorner;
private System.Random mazeRG;
private GameObject[] bricks;
// Use this for initialization
void Start ()
{
mazeRG = new System.Random();
if (mazeWidth % 2 == 0)
mazeWidth++;
if (mazeHeight % 2 == 0)
{
mazeHeight++;
}
maze = new Maze(mazeWidth, mazeHeight, mazeRG);
GenerateMaze();
}
public void GenerateMaze()
{
bricks = GameObject.FindGameObjectsWithTag("MazeBrick");
if (bricks.Length > 0)
DestroyMaze();
maze.Generate();
DrawMaze();
}
private void DestroyMaze()
{
for(int i = 0; i < bricks.Length; i++)
{
DestroyImmediate(bricks[i]);
}
}
void DrawMaze()
{
for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
Vector3 position = new Vector3(x, y);
if (maze.Grid[x, y] == true)
{
CreateMaze(position, transform, 0, mazeRG.Next(0, 3) * 90);
}
}
}
}
void CreateMaze(Vector3 position, Transform parent, int sortingOrder, float rotation)
{
GameObject mazePrefab = Instantiate(wallPrefab, position, Quaternion.identity);
mazePrefab.transform.SetParent(parent);
mazePrefab.transform.Rotate(0, 0, rotation);
mazePrefab.tag = "MazeBrick";
}
// Update is called once per frame
void Update () {
}
}
最后一个检查器中按钮的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MazeGenerator))]
public class GenerateButton : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
MazeGenerator myScript = (MazeGenerator)target;
if (GUILayout.Button("Generate Maze"))
{
myScript.GenerateMaze();
}
}
}
虽然游戏是 运行 每次我单击检查器中的按钮时,它都会在 MazeGeneratore 脚本中生成一个新迷宫:
public void GenerateMaze()
{
bricks = GameObject.FindGameObjectsWithTag("MazeBrick");
if (bricks.Length > 0)
DestroyMaze();
maze.Generate();
DrawMaze();
}
但这会创建新的迷宫,重新站起来。
要一次旋转所有的立方体,我会旋转它们都附加到的父对象。我会简单地在您的 MazeGenerator
脚本的 Start
函数中更改父对象的旋转,因此您的 Start
将如下所示:
void Start ()
{
mazeRG = new System.Random();
if (mazeWidth % 2 == 0)
mazeWidth++;
if (mazeHeight % 2 == 0)
{
mazeHeight++;
}
maze = new Maze(mazeWidth, mazeHeight, mazeRG);
GenerateMaze();
transform.localRotation = Quaternion.Euler(90, 0, 0);
}
当然,您必须在生成迷宫后执行此操作,以确保立方体位置正确。这会将父项的局部旋转设置为沿 X 轴旋转 90 度。
希望能帮到你!
为了使其平整而不是旋转到位,请更改此行(在 MazeGenerator::DrawMaze 中):
Vector3 position = new Vector3(x, y);
收件人:
Vector3 position = new Vector3(x, 0, y);