“Back to Square One” prototype made with Unity and LeanTween
Talking about Back to Square One game, C#, Game development and Unity3D.
Learn cross platform HTML5 game development
Check my Gumroad page for commented source code, games and books.
using UnityEngine;
using System.Collections;
public class MainScript : MonoBehaviour {
// public variables. Values are set in the editor
// the square object
public GameObject squareObject;
// the pivot object
public GameObject pivotObject;
// tansition speed, in seconds
public float transitionSpeed;
// private variables. Values are set in the script
// the hero!!
private GameObject squareHero;
// hero pivot point. Basically another GameObject, the parent of squareHero
private GameObject squareHeroPivot;
// terrain pivot Point, the parent of all terrain tiles
private GameObject terrainPivot;
// can the hero rotate?
private bool canRotate = true;
// array of two elements which will contain ground colors
private Color[] colors = new Color[2];
// this function is executed when the script is initialized.
void Start () {
// filling “colors” array
colors [0] = Color.blue;
colors [1] = Color.green;
// placing squareHero on the centre of the stage
squareHero = Instantiate(squareObject);
// placing squareHeroPivot on the centre of the stage
squareHeroPivot = Instantiate(pivotObject);
// placing the pivot point in the lower right corner of squareHero
squareHeroPivot.transform.position = new Vector2 (0.2f, -0.2f);
// making squareHero child of squareHeroPivot
squareHero.transform.parent = squareHeroPivot.transform;
// now it’s time to place the terrain pivot point
terrainPivot = Instantiate(pivotObject);
// we are going to add 10 terrain tiles, enough for our 320 pixels wide game
for (int i = 0; i < 10; i++) {
// placing a terrain tile on the centre of the stage
GameObject squareTerrain = Instantiate (squareObject);
// placing it accordingly
squareTerrain.transform.position = new Vector2 (-1.6f + i * 0.4f, -0.4f);
// giving the tile a tint color
squareTerrain.GetComponent<Renderer> ().material.color = colors[i % 2];
// making the tile child of the terrain pivot
squareTerrain.transform.parent = terrainPivot.transform;
}
}
// this function is executed at each frame.
void Update () {
// this is how we check if the player clicked
if (Input.GetButtonDown (“Fire1”) && canRotate) {
// setting canRotate to false to prevent the player to rotate the square while it’s already rotating
canRotate = false;
// getting current squareHeroPivot rotation, in degrees
float currentRotation = squareHeroPivot.transform.rotation.eulerAngles.z;
// getting current hero x position
float currentHeroPosition = squareHeroPivot.transform.position.x;
// getting current terrain x position
float currentTerrainPosition = terrainPivot.transform.position.x;
// the core of the script: LeanTween in action
// rotateZ tweens rotation around Z axis, to currentRotation – 90 at “transitionSpeed” speed.
// once completed, tweenComplete function is executed.
LeanTween.rotateZ(squareHeroPivot, currentRotation – 90f, transitionSpeed).setOnComplete(tweenComplete);
// moveX tweens position along x axis, to currentHeroPosition – 0.4 at “transitionSpeed” speed.
LeanTween.moveX (squareHeroPivot, currentHeroPosition – 0.4f, transitionSpeed);
LeanTween.moveX (terrainPivot, currentTerrainPosition – 0.4f, transitionSpeed);
}
}
// tweenComplete function is called when the Z rotation has been completed
void tweenComplete(){
// the player can rotate again
canRotate = true;
// getting square pivot rotation, in degrees
int rotation = (int)squareHeroPivot.transform.eulerAngles.z;
// reposition the pivot so that it’s always in the lower right corner of the square
squareHeroPivot.transform.position = new Vector2 (0.2f, -0.2f);
// changing hero position according to its pivot rotation, ready to start next rotation
// this part can and should be optimized
switch (rotation) {
case 270:
squareHero.transform.localPosition = new Vector2 (-0.2f, -0.2f);
break;
case 180:
squareHero.transform.localPosition = new Vector2 (0.2f, -0.2f);
break;
case 90:
squareHero.transform.localPosition = new Vector2 (0.2f, 0.2f);
break;
case 0:
squareHero.transform.localPosition = new Vector2 (-0.2f, 0.2f);
break;
}
// by default we assume the leftmost child of the tarrain is the first children…
Transform leftmostChild = terrainPivot.transform.GetChild (0).transform;
// … but we loop through all other children comparing their x position to find the actual leftmost child
for (int i = 1; i < 10; i++) {
if (terrainPivot.transform.GetChild (i).transform.position.x < leftmostChild.transform.position.x) {
leftmostChild = terrainPivot.transform.GetChild (i).transform;
}
}
// at this time we move the leftmost child after the rightmost tile, creating an infinite terrain without deleting old tiles and creating new tiles
leftmostChild.transform.position = new Vector2 (2f, -0.4f);
}
}
That’s all at the moment, I plan to turn both this prototype and the Phaser prototype into two simple games, and at the moment you can download the complete project.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.