Oct 282011
 

A touch on-screen automatically works like the click of a mouse, so there is no need to code anything specific until we want to get much fancier.  The button will work by default.

To take it just one step further, let’s add one more button to the bottom.  By adding a few new calculations, we can basically create a new “center” for the accelerometer calculations.   By doing this, you can place your device on a not-so-flat table and account for any tilt.

var dir : Vector3 = Vector3.zero;
private var calibrateX : float = 0;
private var calibrateY : float = 0;

function Update () {

	//In landscape mode, +X is up, -X is down, +Y is left and -Y is right
	dir.x = Input.acceleration.x;
	dir.y = Input.acceleration.y;
	dir.z = Input.acceleration.z;

	if (dir.sqrMagnitude > 1) {
		dir.Normalize();
	}

	//left/right
	transform.position.x = (transform.position.x - (dir.y - calibrateY));

	//up/down
	transform.position.y = (transform.position.y + (dir.x - calibrateX));
}

function OnGUI() {
	GUI.Label(Rect(4,4,300,30), "dir.x: " + dir.x);
	GUI.Label(Rect(4,44,300,30), "dir.y: " + dir.y);
	GUI.Label(Rect(4,84,300,30), "dir.z: " + dir.z);

	//Button to Reset to center position
	if (GUI.Button (Rect(Screen.width /2 - 120, Screen.height - 50, 120, 50), "Reset")) {
	transform.position = Vector3(0,0,0);
	}

 //Button to recalibrate Zeroed control center
 if (GUI.Button (Rect(Screen.width /2 , Screen.height - 50, 120, 50), "Calibrate")) {
 calibrateX = dir.x;
 calibrateY = dir.y;
 }
}

A note regarding OnGUI():

For a good while, I was unable to figure out what was causing lag spiking and slowdown in my Android application.  After some research, I’ve discovered that OnGUI has a tendency to cause some slowdown on both iOS and Android.  From what I understand, the Unity Development team is working to make OnGUI smoother and improved overall, but be sure not to overuse it.  While Update () will run once for every frame, OnGUI will run twice per frame and any calculations or fancy coding within the function might cause issues.  It’s not something to avoid completely, but it’s important to be aware of.

Becoming Published:

Using the tutorials and resources available, you can add any APK file you’ve built into the Android Market.  Signing up to become a developer will simply require a $25 fee (which is much better than Apple’s $99 per year fee), a few screenshots, and a Google account.

If you look at the documentation for a regular developer, you will see that the process seems a bit complex.  Fortunately, Unity has taken care of everything for us.  Our APK files are ready for publication as long as you haven’t checked the boxes that label it as a development build.  Sign up to become a developer and follow the instructions on the screen.

By following through these basic steps, you should be able to take any application you develop and publish it into the wide audience of the Android Market.  In doing this myself for the first time, I found the process quite painless.  A big thanks needs to be credited to the developers of Unity3d for making this software.  Designing games and scripts has quickly become one of my favorite pastimes, and now I have the potential of even making money from doing something that I love.  Of course, there is credit also due to the wonderful people of Google as well for developing Android as well as the Android Market.

 

The next Megabite will delve deeper into the possibilities of Android – now that we are familiar with the overall setup and basics, we can put focus into the actual game creation and attempt to push the limits of our gaming world.  Stay tuned for next Friday, where we will turn our little test application into something a bit more elegant :)

 Leave a Reply

Connect with Facebook