Input for accelerometer and gyroscope
You can access data from the device’s accelerometer using the Vector3 Input.acceleration
property. The coordinates of Input.acceleration
line up with the scene based on the rotation of the device, as shown:
Figure 8.7: The world axes based on screen rotation
Simple examples of this involve moving an object around a scene when the device is moved, using something like the following within an Update()
function on the object:
transform.Translate(Input.acceleration.x, 0, -Input.acceleration.y);
The gyroscope uses more complicated mathematics to get a more precise movement of the screen using the Gyroscope
class. Remember, the gyroscope is not supported on many devices, so it’s best to use the accelerometer when possible.
Note
An example of how to use the gyroscope on an iOS device can be found here: https://docs.unity3d.com/ScriptReference/Gyroscope.html.
Now that we’ve reviewed the various...