Class Accelerometer
Input device representing an accelerometer sensor.
Inherited Members
Namespace: UnityEngine.InputSystem
Syntax
public class Accelerometer : Sensor
Remarks
An accelerometer let's 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.
class MyBehavior : MonoBehaviour
{
protected void OnEnable()
{
// All sensors start out disabled so they have to manually be enabled first.
InputSystem.EnableDevice(Accelerometer.current);
}
protected void OnDisable()
{
InputSystem.DisableDevice(Accelerometer.current);
}
protected void Update()
{
var acceleration = Accelerometer.current.acceleration.ReadValue();
//...
}
}
Properties
acceleration
Declaration
public Vector3Control acceleration { get; }
Property Value
Type | Description |
---|---|
Vector3Control |
current
The accelerometer that was last added or had activity last.
Declaration
public static Accelerometer current { get; }
Property Value
Type | Description |
---|---|
Accelerometer | Current accelerometer or |
Methods
FinishSetup()
Perform final initialization tasks after the control hierarchy has been put into place.
Declaration
protected override void FinishSetup()
Overrides
Remarks
This method can be overridden to perform control- or device-specific setup work. The most common use case is for looking up child controls and storing them in local getters.
public class MyDevice : InputDevice
{
public ButtonControl button { get; private set; }
public AxisControl axis { get; private set; }
protected override void OnFinishSetup()
{
// Cache controls in getters.
button = GetChildControl("button");
axis = GetChildControl("axis");
}
}
MakeCurrent()
Make this the current device of its type.
Declaration
public override void MakeCurrent()
Overrides
Remarks
This method is called automatically by the input system when a device is
added or when input is received on it. Many types of devices have .current
getters that allow querying the last used device of a specific type directly (for
example, see current).
There is one special case, however, related to noise. A device that has noisy controls (i.e. controls for which noisy is true) may receive input events that contain no meaningful user interaction but are simply just noise from the device. A good example of this is the PS4 gamepad which has a built-in gyro and may thus constantly feed events into the input system even if not being actually in use. If, for example, an Xbox gamepad and PS4 gamepad are both connected to a PC and the user is playing with the Xbox gamepad, the PS4 gamepad would still constantly make itself current by simply flooding the system with events.
By enabling filterNoiseOnCurrent (disabled by default),
noise on .current
getters will be filtered out and a device will only see MakeCurrent
getting called if there input was detected on non-noisy controls.
See Also
OnRemoved()
Called by the system when the device is removed from devices.
Declaration
protected override void OnRemoved()
Overrides
Remarks
This is called after the device has already been removed.