Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Sokoban game, 3D and Game development.

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.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.