How to Make an Easy Rng Maze Unity

This is the maze at runtime. There are some enterences/exists I didn't define yet what is enterence and what is exit. This is one problem to decide but for that I need some how to make at least one path I prefer that it will be a random path each time as part of the maze. Single path with enterence and exit.

The maze as it is not don't have one path that the player/object can move in and exit in another exit.

Random Maze

Once the maze has generated it's choosing one enterence randomly and put the player/object with a camera facing that enterence/exit

Now the player by controller or by automation should move in and try to find the path to the other side. Before the path finding how I create a path ? I mean I want to keep the random path's without exist but also to make one random path each time that will have also exit.

Than later the path finding ai but first how to generate a path with the maze ?

This is the maze generator script :

          using System.Collections; using System.Collections.Generic; using UnityEngine;  public class MazeGenerator : MonoBehaviour {     public Maze maze;     public Transform objectToTravel;     public int mazeWidth;     public int mazeHeight;     public string mazeSeed;     public GameObject wallPrefab;     public GameObject waypointPrefab;      private System.Random mazeRG;     public List<Vector3> positions = new List<Vector3>();       // Use this for initialization     void Awake()     {         mazeRG = new System.Random();          if (mazeWidth % 2 == 0)             mazeWidth++;              if (mazeHeight % 2 == 0)             {                 mazeHeight++;             }          maze = new Maze(mazeWidth, mazeHeight, mazeRG);         GenerateMaze();     }      public void GenerateMaze()     {         var bricks = GameObject.FindGameObjectsWithTag("MazeBrick");         var waypoints = GameObject.FindGameObjectsWithTag("Waypoint");          if (bricks.Length > 0)         {             foreach(GameObject brick in bricks)             {                 Destroy(brick);             }         }          if(waypoints.Length > 0)         {             foreach(GameObject waypoint in waypoints)             {                 Destroy(waypoint);             }         }          positions = new List<Vector3>();          maze.Generate();         DrawMaze();     }      void DrawMaze()     {         for (int x = 0; x < mazeWidth; x++)         {             for (int y = 0; y < mazeHeight; y++)             {                 Vector3 position = new Vector3(x, 0.5f, y);                  if (maze.Grid[x, y] == true)                 {                     CreateMaze(position, transform, 0, mazeRG.Next(0, 3) * 90);                 }             }         }          for (int x = 0; x < mazeWidth; x++)         {             for (int y = 0; y < mazeHeight; y++)             {                 Vector3 position = new Vector3(x, 0.5f, y);                  if (maze.Grid[x, y] == false)                 {                     var waypoint = Instantiate(waypointPrefab, new Vector3(x , 0, y), Quaternion.identity);                     waypoint.tag = "Waypoint";                       if (x == 0 || x == mazeWidth - 1 || y == 0 || y == mazeHeight - 1)                     {                         positions.Add(position);                     }                 }             }         }          var randpos = positions[Random.Range(0, positions.Count - 1)];         objectToTravel.position = randpos;          if (randpos.z == 0)         {             objectToTravel.position = new Vector3(objectToTravel.position.x, objectToTravel.position.y, objectToTravel.position.z - 2);             objectToTravel.rotation = Quaternion.identity;             objectToTravel.Rotate(0, 0, 0);         }          if(randpos.x == 0)         {             objectToTravel.position = new Vector3(objectToTravel.position.x - 2, objectToTravel.position.y, objectToTravel.position.z);             objectToTravel.rotation = Quaternion.identity;             objectToTravel.Rotate(0, 90, 0);         }          if(randpos.z == mazeHeight - 1)         {             objectToTravel.position = new Vector3(objectToTravel.position.x, objectToTravel.position.y, objectToTravel.position.z + 2);             objectToTravel.rotation = Quaternion.identity;             objectToTravel.Rotate(0, 180, 0);         }          if(randpos.x == mazeWidth - 1)         {             objectToTravel.position = new Vector3(objectToTravel.position.x + 2, objectToTravel.position.y, objectToTravel.position.z);             objectToTravel.rotation = Quaternion.identity;             objectToTravel.Rotate(0, -90, 0);         }     }      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 () {              } }                  

The maze 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     public int startX;     public 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 = 0;         startY = 0;          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);                 }             }         }     } }                  

And last the Helping Tools 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;     } }                  

evattabor1949.blogspot.com

Source: https://gamedev.stackexchange.com/questions/195120/how-can-i-add-a-exit-to-the-random-maze-generator

0 Response to "How to Make an Easy Rng Maze Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel