# XR Development

## Pressure Cooker: Week 2

So we skipped week 1 because there wasn't really anything we did in development during the [first week](https://johnathan-claasen.gitbook.io/portfolio-johnathan-claasen/xr-concepting-and-design#idea-1). is was basically all designing a concept and testing how it would play out. But getting back to my Individual conept.

### Maze Generation

A very important part of my Maze game was generating a maze. The reason i wanted to generate this and not just make a maze out of manually placed parts replayability (that and not wanting to design a maze). By using the [course ](https://johnathan-claasen.gitbook.io/portfolio-johnathan-claasen/xr-concepting-and-design#the-maze)mentioned in the designing section I was able to learn a bit more about maze generation algorithms. <br>

#### Prim's algorithm:

Starting with prims algorithm: '*Prim's Algorithm is used to find the minimum spanning tree from a graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such that the sum of the weights of the edges can be minimized. Prim's algorithm starts with the single node and explore all the adjacent nodes with all the connecting edges at every step. The edges with the minimal weights causing no cycles in the graph got selected.*'

#### Wilson's algorithm:

The next  algorithm we looked at was Wilsons algorithm '*Wilson’s algorithm uses loop-erased random walks to generate a uniform spanning-tree — an unbiased sample of all possible spanning trees. Most other maze generation algorithms, such as Prim's, random traversal and randomized and randomized depth-first traversal, do not have this property.'*

#### Recursive depth first search:

Recursive depth-first search (DFS) is an algorithm that traverses a graph in search of one or more goal nodes.

DFS was also the algorithm i stuck with in the end and the result looks something like the following.

![The maze using only Cubes for prototyping](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjDo00KSTVw0xTQApG4%2F-MjxUe-ZdkDPn5SxwEYD%2F-MjxUopQV-zUReyWegzU%2Fimage.png?alt=media\&token=721c0bac-ca4f-498d-8141-ae06e57fcce2)

![And finally with the actual maze pieces!](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjDo00KSTVw0xTQApG4%2F-MjxUe-ZdkDPn5SxwEYD%2F-MjxVAxtkavXpYTxucSY%2Fimage.png?alt=media\&token=b9ffc80a-f72d-4e37-a542-308ef9974427)

### Networking and Voice chat

Another important part of this concept was all about playing with other players! This meant i needed some kind of multiplayer.  It was time to import Photon and Photon voice. I wont go into to much detail on photon considering it was a plugin, but it basically works with a lot of custom scripts and using the network callbacks that come with the plugin. After tweaking the project a bit and using the photon scripts i managed to get some multiplayer going which i first tested with a new scene only with a interactable cube.

![](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjDo00KSTVw0xTQApG4%2F-MjxY-VGvRXS-MxzkTBR%2F-MjxYnVgqCauOUiDOzC2%2Fimage.png?alt=media\&token=3ae5fe8c-b915-42a3-920d-6bdd558bf35b)

We started playing arounds with the cube for a while just chilling and chatting.<br>

### Issues?

Having a procedural generated game brought on multiple problems that i hadent really thought about before hand, and being it multiplayer only made it more complex. To start off with when the game started (either by the host or a client) the game would generate a map, which meant every player would have a different maze. This wasnt a great thing because players were walking through walls (which turned out to be a corridoor in their own game). I eventually fixed this by checking if the person starting the game was the host, if so it would generate the maze, otherwise clients would only join.

This led to another problem being that clients werent able to see the maze because it was only instantiated on the host's game. after a while i found it was possible through photon to instantiate  gameobjects over the network which fixed the problem.

The next one was building the navmesh (AI navigation terrain) on a procedurally generated gameobject. It turne out that it wasn't possible to predefine the navmesh on something that is instantiated at run-time. This meant i had to look through every maze piece after generation and rebuild the navmesh manually in a script. This brought another problem of my maze being 30 pieces wide and 30 deep. This meant that i had to rebuild 900 navmeshes which Unity did not like. AT ALL!

After a LONG time of trial and error (about 3 hours to be precise :D) i finally got it working. worst part was by accident which was cool that it worked, but less so that it took me so long and i didn't entirely understand why. So it turns out that I didn't need to loop through all of the maze pieces. Once the entire maze was generated i only needed to rebuild the navmesh for 1 piece of the maze and this automatically generated the navmesh on the entire maze.

![The blue floor is the AI navmesh ](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjDo00KSTVw0xTQApG4%2F-MjxVIfLFMT8xPsoYHWU%2F-MjxVW_91mRfosXnntfD%2Fimage.png?alt=media\&token=d920a8bc-f44c-438b-9c1d-4e84a39fcf0d)

### Spiders! 😕

So i won't go too much into the spiders themselves as it wasn't my own creation, however it was time to make a AI controller that would make the spiders chase the players.&#x20;

When the players would join the game they would be added to a player list and when a spider spawns into the maze it would receive a random target from the spawnlist. By using an NavAgent and the navmesh i was able to give the spiders a destination and they would move to their targets.

Now it was time to add a bit of atmosphere. First of all i made the scene dark, giving it a spooky vibe and added some fog (Pre built into the Universal Render pipeline). Only i found that the fog was making the scene too bright for what i wanted which made me take out the fog and just keep the darkness. All was left was to add the flashlights and spawn some weapons around the maze and players can traverse the maze in the dark, finding weapons that they could use to kill spiders with and find fellow players to wander with and talk to...<br>

![A dead spider in the darkness of the maze.](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjDo00KSTVw0xTQApG4%2F-MjxcbD13lTPMxJAojjK%2F-MjxcjtgY-wFlpgPyCRR%2Fimage.png?alt=media\&token=b4cb170f-f8f4-4b96-bb44-e0ca9be38920)
