Game Development Reference
In-Depth Information
Listing 8.1 Unity JavaScript to Report on Mobile Device
Acceleration
var speed = 10.0;
var dir: Vector3 = Vector3.zero;
function OnGUI()
{
GUI.Label (Rect (10, 10, 100, 20), "x: " + dir.x);
GUI.Label (Rect (10, 30, 100, 20), "y: " + dir.y);
GUI.Label (Rect (10, 50, 100, 20), "z: " + dir.z);
}
function Update ()
{
if(Mathf.Abs(Input.acceleration.x) > 1)
dir.x = Input.acceleration.x;
if(Mathf.Abs(Input.acceleration.y) > 1)
dir.y = Input.acceleration.y;
if(Mathf.Abs(Input.acceleration.z) > 1)
dir.z = Input.acceleration.z;
}
8.4.2 Orientation
As revealed previously, the accelerometer in a device also measures its
orientation. As the accelerometer is fixed with respect to the device, it is
assumed that its orientation is the same. A device can register one of six
orientations: facing up, facing down, upright portrait, upright landscape,
upside-down portrait, and upside-down landscape.
Unity Specifics
Measuring Orientation
As in the previous section, device orientation can be gathered
from the Input class. The orientation value is found in Input
.deviceOrientation. Each of the six orientation positions is catered
for with the values DeviceOrientation.FaceDown, DeviceOrientation
.FaceUp, DeviceOrientation.PortraitUpsideDown, DeviceOrientation
.LandscapeLeft, DeviceOrientation.LandscapeRight, and DeviceOrientation
.Portrait. These orientations relate to the Google Nexus One HTC
mobile as shown in Figure 8.5 .
Adding the script in Listing 8.2 to a new JavaScript file, attaching it to
the Main Camera, and deploying the application to a mobile device will
illustrate the reporting of orientation from within Unity. The orientation
can then be used to move characters and/or flip the player's view.
Search Nedrilad ::




Custom Search