3D Sokoban Prototype with Unity

Read all posts about "" game

If you are following my Sokoban3D series, after seeing the final examples made with Flare3D, Away3D and Alternativa3D, you probably wonder what can be done with Unity, “the most powerful engine this side of a million dollars” as said on the official site.

I played a bit with the engine and I found the learning curve quite soft. Obviously you will need a complete tutorial (which will be published in a couple of days) if you are new to Unity, anyway I tried to keep the script and much similar as possible to the scripts made with the Flash 3D engines.

This is (almost) pure javascript for the game…

var levels:Array=[[1,1,1,1,0,0,0,0],[1,0,0,1,1,1,1,1],[1,0,2,0,0,3,0,1],[1,0,3,0,0,2,4,1],[1,1,1,0,0,1,1,1],[0,0,1,1,1,1,0,0]];		
var wallCube : Transform;
var floorCube : Transform;
var goalCube: Transform;
var crateCube: Transform;
var playerCube:Transform;
var isMoving:boolean;
var isRotating:boolean;
var rotationDir:int;
var movingSteps:int;
var dRow:int;
var dCol:int;
var playerRow:int;
var playerCol:int;
var movingCrate:String;
function Start () {
	isMoving=false;
	isRotating=false;
	var crate:GameObject;
    for (var i = 0; i < 6; i++) {
        for (var j = 0; j < 8; j++) {
			switch (levels[i][j]) {
				case 0:
					Instantiate(floorCube, Vector3 (i, -0.6, j), Quaternion.identity);
					break;
				case 1:
					Instantiate(floorCube, Vector3 (i, -0.6, j), Quaternion.identity);
					Instantiate(wallCube, Vector3 (i, 0, j), Quaternion.identity);
					break;
				case 2:
					Instantiate(goalCube, Vector3 (i, -0.6, j), Quaternion.identity);
					break;
				case 3:
					Instantiate(floorCube, Vector3 (i, -0.6, j), Quaternion.identity);
					var crateCube=Instantiate(crateCube, Vector3 (i, 0, j), Quaternion.identity);
					crateCube.name="crate_"+i+"_"+j;
					break;
				case 4:
					Instantiate(floorCube, Vector3 (i, -0.6, j), Quaternion.identity);
					Instantiate(playerCube, Vector3 (i,0, j), Quaternion.identity);
					playerRow=i;
					playerCol=j;
					break;
			}
        }
    }
} 
function Update() {
	var thePlayer:GameObject=GameObject.FindWithTag("Player");
	if(!isMoving && !isRotating){
		if(Input.GetKeyDown (KeyCode.UpArrow)){
			switch(Mathf.Round(thePlayer.transform.eulerAngles.y)){
				case 0 :
					dRow=0;
					dCol=-1;
					break;
				case 90 :
					dRow=-1;
					dCol=0;
					break;
				case 180 :
					dRow=0;
					dCol=1;
					break;
				case 270 :
					dRow=1;
					dCol=0;
					break;
			}
			if (levels[playerRow+dRow][playerCol+dCol]==0 ||levels[playerRow+dRow][playerCol+dCol]==2){
				isMoving=true;
				movingSteps=0;
			}
			else{
					if (levels[playerRow+dRow][playerCol+dCol]==3||levels[playerRow+dRow][playerCol+dCol]==5) {
						if (levels[playerRow+2*dRow][playerCol+2*dCol]==0||levels[playerRow+2*dRow][playerCol+2*dCol]==2) {
							movingCrate="crate_"+(playerRow+dRow)+"_"+(playerCol+dCol);
							isMoving=true;
							movingSteps=0;
						}
					}						
			}
		}
		else{
			if(Input.GetKeyDown (KeyCode.RightArrow)){
				isRotating=true;
				rotationDir=5;
			}
			else{
				if(Input.GetKeyDown (KeyCode.LeftArrow)){
					isRotating=true;
					rotationDir=-5;
				}
			}
		}
	}
	if(isMoving==true){
		thePlayer.transform.Translate(Vector3.forward *-0.1);
		if(movingCrate){
			var theCrate:GameObject=GameObject.Find(movingCrate);
			switch(Mathf.Round(thePlayer.transform.eulerAngles.y)){
				case 0 :
					theCrate.transform.Translate(Vector3.forward *-0.1);
					break;
				case 90 :
					theCrate.transform.Translate(Vector3.right *-0.1);
					break;
				case 180 :
					theCrate.transform.Translate(Vector3.forward *0.1);
					break;
				case 270 :
					theCrate.transform.Translate(Vector3.right *0.1);
					break;
			}
		}
		movingSteps++;
		if(movingSteps==10){
			isMoving=false;
			levels[playerRow+dRow][playerCol+dCol]+=4;
			levels[playerRow][playerCol]-=4;
			if(movingCrate){
				levels[playerRow+2*dRow][playerCol+2*dCol]+=3;
				levels[playerRow+dRow][playerCol+dCol]-=3;
				theCrate.name="crate_"+(playerRow+2*dRow)+"_"+(playerCol+2*dCol);
				movingCrate="";
			}
			playerRow+=dRow;
			playerCol+=dCol;
			
		}
	}
	if(isRotating==true){
		thePlayer.transform.Rotate(0,rotationDir,0);	
		if(Mathf.Round(thePlayer.transform.eulerAngles.y)%90==0){
			isRotating=false;
		}
	}
}

And this is the script which controls the camera:

function Update() {
	target=GameObject.FindWithTag("Player").transform;
	var angleToReach:Number = Mathf.LerpAngle(transform.eulerAngles.y,target.eulerAngles.y-180,4*Time.deltaTime);
	var currentRotation:Quaternion = Quaternion.Euler(0,angleToReach,0);
	transform.position = target.position+Vector3(0,9,0);
	transform.position -= currentRotation*Vector3.forward*4;
	transform.LookAt (target.position+Vector3(0,3,0));
}

Finally, this is the result:

LEFT, RIGHT and UP arrow to play. Next, I'll prepare the textured version then a tutorial will follow.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers