Simple Unity Dungeon Maker Script


This script is placed on a plane and creates a 15 by 15 grid of prefabs based on a csv-like file. It can be used to create a dungeon like map.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class MultiDimensionalInt
    public int[] intArray;

public class MapMaker : MonoBehaviour { 
    public bool hideWall = false;
    private bool hist = false;
    public Transform player;
    public float disappearThresh;
    public GameObject[] walls;
    public float[] yoffset;
    public float[] xoffset;
    public float[] zoffset;

    public TextAsset mapData;
    public string[][] map = new string[15][];
    void Start()
        string[] text = mapData.text.Split('\n');
        for (int i = 0; i < map.Length; i++)
            map[i] = text[i].Split(',');

        if (!hideWall)
    void Update()
        if (hideWall != hist)
            if (hideWall)
            } else
            hist = hideWall;

        if (Vector3.Distance(transform.position, player.position) > disappearThresh)
            hideWall = true;
        } else if (Vector3.Distance(transform.position, player.position) < disappearThresh)
            hideWall = false;

    void loadWalls()
        for (int i = -14; i <= 14; i += 2)
            for (int j = -14; j <= 14; j += 2)
                int res = Convert.ToInt32(map[(i + 14) / 2][ (j + 14) / 2]);
                if (res != 0)
                    int index = res - 1;
                    var wall = Instantiate(walls[index], transform.position + (new Vector3(i + xoffset[index], yoffset[index], j + zoffset[index])), Quaternion.identity);
                    wall.transform.parent = transform;

    void unloadWalls()
        foreach (Transform child in transform)


Raycasting is used to create a 3D projection of a 2D scene. Raycasting can be seen in games like Wolfenstein 3D, and was used because it wasn't possible to run 3D engines in real time, when computers were slower. Raycasting saves computing power by only casting one ray per vertical strip on the screen.

In this project the map of the game consists of walls, a floor, and ceiling. This is represented by a 2-dimensional array of integers. Each integer represents a cube section of a wall. e.g. 0 would indicate no wall, 1 would indicate a red wall And the player position, direction are defined using vectors. And either a field of view (a floating point number) or projection plan (a vector) is defined.

Using this information, a ray (a ray starts at one point and goes off in a direction for infinity) for each vertical section of the screen is cast out until it hits a wall. The correct vertical section of the wall is then drawn to the screen and scaled down depending on how far it is from the player.
For more information and tutorials see links at the bottom.

360 Panorama of Raycasting

Raycasting code (for Processing)

for (int x = 0; x < width; x++) {
	//gives a value from -1 to 1 to each x coordinate
	float planePosition = (2 * x / (float)width - 1) * -1;
	//direction vector of ray
	Vector2D ray = new Vector2D(direction.x * cos(fov * planePosition) - direction.y * sin(fov * planePosition), direction.x * sin(fov * planePosition) + direction.y * cos(fov * planePosition));

	//position in array
	int mapX = (int)position.x;
	int mapY = (int)position.y;

	//DDA algorithm (used to find the wall)
	Vector2D distanceToNextSquare = new Vector2D(0, 0);
	Vector2D nextSquareIncrement = new Vector2D(sqrt(1 + (ray.y * ray.y) / (ray.x * ray.x)), sqrt(1 + (ray.x * ray.x) / (ray.y * ray.y)));
	Vector2D mapStep = new Vector2D(0, 0);
	boolean XY = false;

	if (ray.x < 0)
		mapStep.x = -1;
		distanceToNextSquare.x = (position.x - mapX) * nextSquareIncrement.x;
		mapStep.x = 1;
		distanceToNextSquare.x = (mapX + 1.0 - position.x) * nextSquareIncrement.x;
	if (ray.y < 0)
		mapStep.y = -1;
		distanceToNextSquare.y = (position.y - mapY) * nextSquareIncrement.y;
		mapStep.y = 1;
		distanceToNextSquare.y = (mapY + 1.0 - position.y) * nextSquareIncrement.y;
	while (true) {
		if (distanceToNextSquare.x < distanceToNextSquare.y)
			distanceToNextSquare.x += nextSquareIncrement.x;
			mapX += mapStep.x;
			XY = false;
			distanceToNextSquare.y += nextSquareIncrement.y;
			mapY += mapStep.y;
			XY = true;

		if (mapX < 0 || mapY < 0 || mapX >= mapSize || mapY >= mapSize) {
			//ray now outside of array
		} else if (map[mapX][mapY] > 0) {
			//wall found

			//calculate distance
			float distance = 0;
		if (XY) {
			distance = mapY - position.y; 
			if (ray.y < 0)
				distance /= ray.y;
			} else {
				distance = mapX - position.x;
			if (ray.x < 0)
				distance /= ray.x;

			//fisheye correction
			distance = fishEyeCorrection ? distance * abs(cos(fov * planePosition)) : distance;

			//wall segment size
			int drawStart = -(int)(height / distance) / 2 + height / 2;
			int drawEnd = (int)(height / distance) / 2 + height / 2;

			//get color and darken color for back faces to create shading
			if (XY) {
				color c;
			if (map[mapX][mapY] > colors.length || map[mapX][mapY] <= 0)
				c = color(#0AFFD4);
				c = colors[map[mapX][mapY] - 1];

			float r = red(c);
			float b = blue(c);
			float g = green(c);

			stroke(color(r / 2, g / 2, b / 2)); 

		} else
			if (map[mapX][mapY] > colors.length || map[mapX][mapY] <= 0)
				stroke(colors[map[mapX][mapY] - 1]);

			//draw line
			line(x, drawStart, x, drawEnd);


Fish Eye effect

The fish eye effect is created when the distance the ray has to travel to get to a wall is farther than the actual distance to the wall from the player.

You can see how when uncorrected the walls bulge in the center. When correction is turned on the wall becomes straight again. However the sides of the screen become distorted the larger the field of view is and does not work past 90 degrees field of view.


360 Degree View: Click Here

Raycasting Info
Lodev: Click Here
Permandi: Click Here
PlayfulJS: Click Here 
Processing: Click Here

HTML5 Spatial Audio

When messing around with Phaser I wanted a way to use HTML5 3D audio. 3D audio allows people using headphones to hear noises coming from different directions.

Instead of creating a phaser plugin, I instead created a stand alone library that you can interface with phaser. This means that this library can be used with other game frameworks or by itself. The library is essentially a wrapper for the HTML 3D audio api. The library uses code from here to load sounds.

Loading Sounds

 // Creates the buffer to load sounds - createBufferLoader(array of sound locations, function to run after sounds are loaded)
    ], function(){

// load sounds

//is true if sounds have loaded

Manipulating Listener (the listener is like where the persons ears would be in the scene)

//Set the listener's orientation - setListOrientation(x, y, z, x up vector, y up vector, z up vector)
audioLIB.setListOrientation(x, y, z, Ux, Uy, Uz);

//Set the listener's position
audioLIB.setListPosition(x, y, z);

//Set the listener's velocity
audioLIB.setListVelocity(x, y, z);

//Update the listener's orientation and position based on a sprite - updateFromSprite(sprite, [optional] sprite body)
//Doesn't set the listener's velocity

Mainpulationg Sounds

//Array containing all sounds

//Shortcut for setting whether the sounds should loop or not - setSoundLoops(boolean)

//Starts playing all sounds

//Stops playing all sounds

//Runs a function for each sound
audioLIB.forEachSound(function (sound) {

//Functions in sound objects

//Set sound object orientation, position and velocity

//Set sound object direction - setDirection(coneInnerAngle, coneOuterAngle, coneOuterGain)
audioLIB.Sound.prototype.setDirection(iA, oA, oG);

//Set sound object gain - setGain(float)

//Set whether sound object loops - setLoop(boolean)

//Set the sound object's buffer (the buffer contains the sound objects sound data)

//Update the sound object's orientation and position with a sprite - updateFromSprite(sprite, [optional] sprite body)
audioLIB.Sound.prototype.updateFromSprite(sprite, body);

//Start playing the sound at position v

//Stop playing the sound
Download Link: Click here
Please credit me if used

Download Link for BufferLoader: Click here


An asteroids game made with Phaser. Phaser is a HTML5 game framework. It allows you to create 2D games for websites. 

You fly around as a space ship trying to avoid the asteroids, and shoot at them to gain points. When you shoot a large asteroid it splits into smaller asteroids, and shooting a smaller asteroid will destroy it. Objects wrap around screen edges, for example if the ship goes off the left edge it appears on the right edge.

The game is based on Asteroids, an arcade game made in the 70s. It had the same aesthetic as the original game. The arcade game also had flying saucers, which would attack the player, and a hyperdrive that would place the player randomly on the screen.

Click here to play the game

Libraries Used: Click Here
Ateroids Arcade Game: Click Here