Unity Project

This tiny game demonstrates the primary features of a video game, where you control the player’s movements and the Unity game engine determines what happens in a collision.

The object of this game is to roll the ball into the cubes, either by tilting the device or pushing the ball with a finger. This version is geared toward an Android phone, but it can run on any device with a Unity driver.

During the mini-game!

When a cube is touched by the ball, the C# code in PlayerController.cs performs a simple game action – playing a sound (Homer Simpson saying "Woo-hoo!") and destroying that cube – see the code listing below.

The cubes have a different picture on each face, and the playing field is Easter Island.  The ball is actually white with a reddish-purple light source shining on it, and the game camera follows the ball rather than staying put over the game field. The walls are now glued down – in the first iteration, as the ball and each wall weigh 1 Unity mass unit, a ball bounce knocked that wall clean off!


using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour   // this controller code is attached to the player object (in this case the ball)
	public float speed = 300.0f;            // overall game running speed factor
	public float touchSpeed = 1.0f;         // touch input speed factor
	public GUIText countTxt;                // display string for cubes-to-go -- GUIText variables map to Unity scene text variables
	public GUIText winTxt;                  // display string for winning!
	public int maxcount = 5;                // number of target cubes

	private int count;                      // count of cubes-to-go
	private string xaxe = "Horizontal";     // Unity-specific axis names, for example joystick axes
	private string yaxe = "Vertical";
	private Vector3 gohere;                 // next ball movement's vector
	private GameObject go;                  // each cube is a game object
	float horiz, vert;                      // incoming movement axis values

	void Start ()                           // runs once at startup
		winTxt.enabled = false;		
		SetCountTxt ();

		if (Application.platform == RuntimePlatform.Android) {
			Screen.orientation = ScreenOrientation.LandscapeLeft;
			Screen.sleepTimeout = SleepTimeout.NeverSleep;
			Screen.autorotateToLandscapeLeft = true;

		for (int i = 0; i < maxcount; i++) {  // instantiate cubes
			go = (GameObject)Instantiate (Resources.Load ("MyCube"), 
new Vector3 (Random.Range (-9.0F, 9.0F), 1.0f, Random.Range (-9.0F, 9.0F)),
Quaternion.identity); } } void Update () // runs once per game frame, such as 30fps { if (Input.GetKey (KeyCode.Escape)) { Application.Quit (); } // check whether there's been a moving touch on the screen; if so, move the ball correspondingly if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) { Vector2 touchDeltaPosition = Input.GetTouch (0).deltaPosition * touchSpeed; moveMe (touchDeltaPosition.x, touchDeltaPosition.y); } } void FixedUpdate () // runs once per physics time tick, by default 0.02 seconds { if (SystemInfo.deviceType == DeviceType.Handheld) { gohere = Input.acceleration; // acceleration vector from mobile device's built-in gyro if (gohere.sqrMagnitude > 1) gohere.Normalize (); horiz = gohere.x; vert = gohere.y; } else { horiz = Input.GetAxis (xaxe); // use the default Unity input device, possibly a mouse or joystick vert = Input.GetAxis (yaxe); } moveMe (horiz, vert); } void moveMe (float horiz, float vert) // adds the movement vector to the current ball vector { Vector3 moveme = new Vector3 (horiz, 0.0f, vert); GetComponent<Rigidbody> ().AddForce (moveme * speed * Time.deltaTime); } void OnTriggerEnter (Collider other) // triggered whenever the ball collides with another object { if (other.name.Contains ("Target")) { // was the object one of the Target cubes? GetComponent<AudioSource> ().Play (); // play happy sound (Homer Simpson saying "Woo-hoo!") Destroy (other.gameObject); // obliterate that cube count++; SetCountTxt (); } // "else" could be one of the walls, if we wanted the walls to actively influence the game } void SetCountTxt () { countTxt.text = (maxcount - count) + " mini-cubes to go!"; if (count >= maxcount) { // all cubes are gone countTxt.enabled = false; // disable counting text visibility winTxt.enabled = true; // show winning text (its value is set in the Unity scene editor, in this case "Nice going!") } } }