Sensor Support
Sensors are InputDevices
measuring environmental characteristics of the device playing the content. Sensors are currently supported on iOS and Android (with Android supporting a wider range of sensors then iOS).
Unlike other devices, sensors start out being disabled. To enable a sensor, call InputSystem.EnableDevice()
).
InputSystem.EnableDevice(Gyroscope.current);
To disable a sensor again, call InputSystem.DisableDevice()
.
InputSystem.DisableDevice(Gyroscope.current);
To check whether a sensor is currently enabled, use InputDevice.enabled
.
if (Gyroscope.current.enabled)
Debug.Log("Gyroscope is enabled");
Each sensor device implements a single control which represents the data read by the sensor. The following Sensors are available:
Device | Android | iOS | Control | Type |
---|---|---|---|---|
Accelerometer |
Yes | Yes | acceleration |
Vector3Control |
Gyroscope |
Yes | Yes | angularVelocity |
Vector3Control |
GravitySensor |
Yes | Yes | gravity |
Vector3Control |
AttitudeSensor |
Yes | Yes | attitude |
QuaternionControl |
LinearAccelerationSensor |
Yes | Yes | acceleration |
Vector3Control |
MagneticFieldSensor |
Yes | No | magneticField |
Vector3Control |
LightSensor |
Yes | No | lightLevel |
AxisControl |
PressureSensor |
Yes | No | atmosphericPressure |
AxisControl |
ProximitySensor |
Yes | No | distance |
AxisControl |
HumiditySensor |
Yes | No | relativeHumidity |
AxisControl |
AmbientTemperatureSensor |
Yes | No | ambientTemperature |
AxisControl |
StepCounter |
Yes | No | stepCounter |
IntegerControl |
Sampling Frequency
Sensors sample continuously at a set interval. The sampling frequency for each sensors can queried or set using the samplingFrequency
property. The frequency is expressed in Hertz (i.e. number of samples per second).
// Get sampling frequency of gyro.
var frequency = Gyroscope.current.samplingFrequency;
// Set sampling frequency of gyro to sample 16 times per second.
Gyroscope.current.samplingFrequency = 16;
Accelerometer
An accelerometer lets you measure the acceleration of a device, and can be useful to control content by moving a device around. Note that the accelerometer will report the acceleration measured on a device both due to moving the device around, and due gravity pulling the device down. You can use GravitySensor
and LinearAccelerationSensor
to get decoupled values for these.
Gyroscope
A gyroscope let's you measure the angular velocity of a device, and can be useful to control content by rotating a device.
GravitySensor
A gravity sensor let's you determine the direction of the gravity vector relative to a device, and can be useful to control content by device orientation. This is usually derived from a hardware Accelerometer
, by subtracting the effect of linear acceleration (see LinearAccelerationSensor
).
AttitudeSensor
An attitude sensor let's you determine the orientation of a device, and can be useful to control content by rotating a device.
LinearAccelerationSensor
An accelerometer let's you measure the acceleration of a device, and can be useful to control content by moving a device around. Linear acceleration is the acceleration of a device unaffected by gravity forces. This is usually derived from a hardware Accelerometer
, by subtracting the effect of gravity (see GravitySensor
).
MagneticFieldSensor
Input device representing the magnetic field affecting the device playing the content. Values are in micro-Tesla (uT) and measure the ambient magnetic field in the X, Y and Z axis.
LightSensor
Input device representing the ambient light measured by the device playing the content. Value is in SI lux units.
PressureSensor
Input device representing the atmospheric pressure measured by the device playing the content. Value is in in hPa (millibar).
ProximitySensor
Input device representing the proximity of the device playing the content to the user. The proximity sensor is usually used by phones to determine if the user is holding the phone to their ear or not. Values represent distance measured in centimeters.
HumiditySensor
Input device representing the ambient air humidity measured by the device playing the content. Values represent the relative ambient air humidity in percent.
AmbientTemperatureSensor
Input device representing the ambient air temperature measured by the device playing the content. Values represent temperature in degree Celsius.
StepCounter
Input device representing the foot steps taken by the user as measured by the device playing the content.