Class CompassSensor
Input device representing the device's compass, reporting heading relative to magnetic and true north.
Inherited Members
Namespace: UnityEngine.InputSystem
Assembly: Unity.InputSystem.dll
Syntax
public class CompassSensor : Sensor
Remarks
Readings are only valid while the device is enabled through EnableDevice(InputDevice). Whether trueHeading requires a running location service to be meaningful is platform-dependent. On Android, it falls back to magneticHeading unless a recent location fix is available (when LocationSensor is started). Behavior on other platforms may differ.
Don't drive heading updates from both this device and the legacy UnityEngine.Input.compass API in
the same project. Both share the same underlying platform heading service, so disabling this device also
stops updates for the legacy API (the reverse is also true). Use a single compass API for each project.
Properties
current
The last compass sensor either added or active.
Declaration
public static CompassSensor current { get; }
Property Value
| Type | Description |
|---|---|
| CompassSensor | Current compass sensor or |
headingAccuracy
Accuracy of the heading reading in degrees.
Declaration
public AxisControl headingAccuracy { get; protected set; }
Property Value
| Type | Description |
|---|---|
| AxisControl | A negative value indicates the reading is unreliable. |
magneticHeading
Heading in degrees relative to magnetic north.
Declaration
public AxisControl magneticHeading { get; protected set; }
Property Value
| Type | Description |
|---|---|
| AxisControl | 0 is north, 90 is east, 180 is south, and 270 is west. |
rawVector
Raw geomagnetic field measured by the magnetometer, in microteslas.
Declaration
public Vector3Control rawVector { get; protected set; }
Property Value
| Type | Description |
|---|---|
| Vector3Control | Geomagnetic field on the x, y, and z device axes. |
timestamp
Time at which the last heading was measured, in seconds.
Declaration
public DoubleControl timestamp { get; protected set; }
Property Value
| Type | Description |
|---|---|
| DoubleControl | Timestamp of the most recent reading, as measured by the underlying sensor hardware. On the web platform, no native measurement time is available, so this reflects the time the reading was received instead. |
trueHeading
Heading in degrees relative to geographic (true) north.
Declaration
public AxisControl trueHeading { get; protected set; }
Property Value
| Type | Description |
|---|---|
| AxisControl | On Android, meaningful only while a recent location fix is available (when LocationSensor is started); otherwise equal to magneticHeading. Behavior on other platforms may differ. |
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.
Examples
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. Hence why by default, noise on .current getters
will be filtered out and a device will only see MakeCurrent getting called if their 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
Examples
using UnityEngine.InputSystem;
public class MyDevice : InputDevice
{
public static MyDevice current { get; private set; }
protected override void OnRemoved()
{
// use this context to unassign the current device for instance
base.OnRemoved();
if (current == this)
current = null;
}
}