Introduction: Practiced Hand demo, "Out of the Maze", the article mainly talk about how to create a maze.
Learning Unity3d For some time, I think of a project to practiced hand hand. And then there's this.
A fixed number of lattice, after the start of random generation.
Description: This maze 10*10, after the start of random generation, surrounded by an empty exit.
First say how to achieve:
- The main prepared three prefab: horizontal wall, vertical wall, pillar, wall height is 10, width is 10, thickness is 1, column height is 10. Both width and thickness are 10.
Manually arranged by 10*10 (reference)
#region//Initializing the game#endregionusingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public classstartset:monobehaviour{ Publicgameobject[] Aroundwalls; Publicgameobject[] walls; Public intMaxi=2; //Use this for initialization voidStart () {//Export intI=random.range (1, aroundwalls.length); Aroundwalls[i]. SetActive (false); //Create a Maze for(intj =0; J < Walls. Length; J + +) { intx = Random.range (0, Maxi); if(x==0) {Walls[j]. SetActive (false); } Else{walls[j]. SetActive (true); } } }}
View Code
- Drag the wall around to around Walls, inside the wall dragged to Walls.
Here are some of the graphs:
Below, the script Startset to the camera, you can start the game to see the effect.
Here are some of the graphs that I've run here:
Summary:
- Vertical wall/transverse wall. In fact, it is the same, only the angle difference 90. I've made two of them handy for scripting code.
- There is no path to such a maze, it is completely random. Not necessarily to get out of the export. My idea is that you can have a character with a function like a "breakdown wall".
- public int maxi=2 If you increase this number, you can make the maze difficult to upgrade. My initial idea was to make maxi=1, but not actually.
Two. Random Size maze
Referring to the above, I continue to think, can be made into a random size maze.
For example, I input 10, generate a 10*10 maze to me, enter 25, generate a 25*25 maze for me.
Continue the above ideas, create "lattice", and then use the above script to modify into a maze.
- First look at the effect: 13*13,27*27,50*50, respectively.
2. How to Achieve
- Or three prefab, horizontal wall, vertical wall, pillar. Here the size is 10 times times bigger than the above, lazy less count point decimal ^.^.
- Script:
1 usingUnityengine;2 3 /// <summary>4 ///Initializing the Maze5 /// </summary>6 Public classInitializemaze:monobehaviour7 {8 PublicGameobject Wallh;9 PublicGameobject wallv;Ten PublicGameobject pillar; One Public intSidenumber = -; A - //Use this for initialization - voidAwake () the { - intSidexy = Sidenumber * One; - - for(inti =0; I < (Sidenumber +1); i++) + { - for(intj =0; J < (Sidenumber +1); J + +) + { A intPillarx = i * the; at intWALLHX = -+ the*i; - intPillarz = J * the; - intWallvz = -+ the*J; - - if(i== sidenumber && j==sidenumber) - { inInstantiate (Pillar,NewVector3 (Pillarx, -, Pillarz), quaternion.identity); - Continue; to } + if(i = =sidenumber) - { theInstantiate (Pillar,NewVector3 (Pillarx, -, Pillarz), quaternion.identity); *Instantiate (WALLV,NewVector3 (Pillarx, -, Wallvz), quaternion.identity); $ Continue ;Panax Notoginseng } - if(j==sidenumber) the { +Instantiate (Pillar,NewVector3 (Pillarx, -, Pillarz), quaternion.identity); AInstantiate (WALLH,NewVector3 (WALLHX, -, Pillarz), quaternion.identity); the } + Else - { $Instantiate (Pillar,NewVector3 (Pillarx, -, Pillarz), quaternion.identity); $Instantiate (WALLH,NewVector3 (WALLHX, -, Pillarz), quaternion.identity); -Instantiate (WALLV,NewVector3 (Pillarx, -, Wallvz), quaternion.identity); - } the } - }Wuyi } the}
View Code
3. Testing
Drag the three prefab to the corresponding position in the script and enter the edges in the sidernumber. Start the game and you'll get the grid.
After getting the grid, the first example of the Startset code, add a piece of code to find Gameobject, you can get the desired maze (you can try, the code is not affixed)
Summarize:
1. It is interesting to create a mesh when, very around people, need to remove the edge corner. Before going to bed, the inspiration suddenly came, and figured out, quite interesting.
2. quaternion This API can not understand, first put in the study list, if this, should be able to cross the wall, vertical wall only with one.
Level Limited, welcome advice: [Email protected]
Wishing: I hope to find a full-time game development work.
[Game development-Learning notes] Rookie slowly Fly (ii)-Maze