docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class LocationSensor

    Input device representing a GPS location sensor.

    Inheritance
    object
    InputControl
    InputDevice
    Sensor
    LocationSensor
    Inherited Members
    Sensor.samplingFrequency
    InputDevice.InvalidDeviceId
    InputDevice.description
    InputDevice.enabled
    InputDevice.canRunInBackground
    InputDevice.added
    InputDevice.remote
    InputDevice.native
    InputDevice.updateBeforeRender
    InputDevice.deviceId
    InputDevice.lastUpdateTime
    InputDevice.wasUpdatedThisFrame
    InputDevice.allControls
    InputDevice.valueType
    InputDevice.valueSizeInBytes
    InputDevice.ReadValueFromBufferAsObject(void*, int)
    InputDevice.ReadValueFromStateAsObject(void*)
    InputDevice.ReadValueFromStateIntoBuffer(void*, void*, int)
    InputDevice.CompareValue(void*, void*)
    InputDevice.OnConfigurationChanged()
    InputDevice.ExecuteCommand<TCommand>(ref TCommand)
    InputDevice.ExecuteCommand(InputDeviceCommand*)
    InputControl.name
    InputControl.displayName
    InputControl.shortDisplayName
    InputControl.path
    InputControl.layout
    InputControl.variants
    InputControl.device
    InputControl.parent
    InputControl.children
    InputControl.usages
    InputControl.aliases
    InputControl.stateBlock
    InputControl.noisy
    InputControl.synthetic
    InputControl.this[string]
    InputControl.magnitude
    InputControl.ToString()
    InputControl.EvaluateMagnitude()
    InputControl.EvaluateMagnitude(void*)
    InputControl.WriteValueFromBufferIntoState(void*, int, void*)
    InputControl.WriteValueFromObjectIntoState(object, void*)
    InputControl.TryGetChildControl(string)
    InputControl.TryGetChildControl<TControl>(string)
    InputControl.GetChildControl(string)
    InputControl.GetChildControl<TControl>(string)
    InputControl.RefreshConfigurationIfNeeded()
    InputControl.RefreshConfiguration()
    InputControl.m_StateBlock
    InputControl.currentStatePtr
    InputControl.previousFrameStatePtr
    InputControl.defaultStatePtr
    InputControl.noiseMaskPtr
    InputControl.stateOffsetRelativeToDeviceRoot
    InputControl.optimizedControlDataType
    InputControl.CalculateOptimizedControlDataType()
    InputControl.ApplyParameterChanges()
    Namespace: UnityEngine.InputSystem
    Assembly: Unity.InputSystem.dll
    Syntax
    public class LocationSensor : Sensor
    Remarks

    A location sensor reports the device's geographic position (latitude, longitude, altitude). After enabling it with EnableDevice(InputDevice), the location service may take several seconds to acquire valid data, so readings are only valid once status reaches Running. Accessing location requires the user to have granted permission (isEnabledByUser). The sensor's presence (current being non-null) does not guarantee the location service is available or running; query status and isEnabledByUser to determine that.

    Do not drive location from both this device and the legacy UnityEngine.Input.location API in the same project. Both share the same underlying platform location service, so disabling this device also stops updates for the legacy API (and vice versa). Use a single location API per project.

    On Android and iOS the required location permission (Android) and usage description (iOS) are added to the build automatically only when your compiled code references LocationSensor directly. If you access the sensor solely through an .inputactions asset binding and never reference the type in code, this detection does not trigger and the build ships without them, so the service fails to start at runtime. Reference LocationSensor in code (for example by accessing current once), or add the platform permission and usage description manually.

    Properties

    altitude

    Altitude in meters.

    Declaration
    public AxisControl altitude { get; protected set; }
    Property Value
    Type Description
    AxisControl

    current

    The location sensor that was last added or had activity last.

    Declaration
    public static LocationSensor current { get; }
    Property Value
    Type Description
    LocationSensor

    Current location sensor or null.

    horizontalAccuracy

    Horizontal accuracy of the reading in meters.

    Declaration
    public AxisControl horizontalAccuracy { get; protected set; }
    Property Value
    Type Description
    AxisControl

    isEnabledByUser

    Whether the user has granted the app permission to access the device location.

    Declaration
    public bool isEnabledByUser { get; }
    Property Value
    Type Description
    bool
    Remarks

    A user can grant permission while the service is not running. If permission is denied, the service won't reach Running. Must be accessed from the main thread only.

    latitude

    Latitude in degrees.

    Declaration
    public AxisControl latitude { get; protected set; }
    Property Value
    Type Description
    AxisControl

    longitude

    Longitude in degrees.

    Declaration
    public AxisControl longitude { get; protected set; }
    Property Value
    Type Description
    AxisControl

    status

    Current status of the location service.

    Declaration
    public LocationServiceStatus status { get; }
    Property Value
    Type Description
    LocationServiceStatus
    Remarks

    After the sensor is enabled the service starts asynchronously, passing through Initializing before it reaches Running. Readings are only valid while running. Must be accessed from the main thread only.

    timestamp

    Time the reading was taken, in seconds since the epoch used by the platform location service.

    Declaration
    public DoubleControl timestamp { get; protected set; }
    Property Value
    Type Description
    DoubleControl

    verticalAccuracy

    Vertical accuracy of the reading in meters.

    Declaration
    public AxisControl verticalAccuracy { get; protected set; }
    Property Value
    Type Description
    AxisControl

    Methods

    Configure(float, float)

    Sets the desired accuracy and update-distance threshold for location readings.

    Declaration
    public void Configure(float desiredAccuracyInMeters, float updateDistanceInMeters)
    Parameters
    Type Name Description
    float desiredAccuracyInMeters

    Desired horizontal accuracy, in meters.

    float updateDistanceInMeters

    Minimum distance the device must move before a new reading is reported, in meters.

    Remarks

    By default, the sensor is configured with the values from locationAccuracy and locationDistanceThreshold. ResetConfiguration() returns to those defaults.

    If the sensor is already enabled, readings may briefly pause while they are applied. If the sensor is disabled, values apply when the device is enabled. Must be called from the main thread only.

    FinishSetup()

    Perform final initialization tasks after the control hierarchy has been put into place.

    Declaration
    protected override void FinishSetup()
    Overrides
    InputControl.FinishSetup()
    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
    InputDevice.MakeCurrent()
    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
    current
    current
    current
    current

    OnAdded()

    Called by the system when the device is added to devices.

    Declaration
    protected override void OnAdded()
    Overrides
    InputDevice.OnAdded()
    Remarks

    This is called after the device has already been added. devicesAddedOnRemoved()

    Examples
      using UnityEngine.InputSystem;
    
                                                                                                                                                                                                                                  public class MyDevice : InputDevice
                                                                                                                                                                                                                                  {
                                                                                                                                                                                                                                      public static MyDevice current { get; private set; }
                                                                                                                                                                                                                                      protected override void OnAdded()
                                                                                                                                                                                                                                      {
                                                                                                                                                                                                                                          // use this context to assign the current device for instance
                                                                                                                                                                                                                                          base.OnAdded();
                                                                                                                                                                                                                                          current = this;
                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                  }
    

    OnRemoved()

    Called by the system when the device is removed from devices.

    Declaration
    protected override void OnRemoved()
    Overrides
    InputDevice.OnRemoved()
    Remarks

    This is called after the device has already been removed. devicesRemovedOnAdded()

    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;
                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                  }
    

    ResetConfiguration()

    Reverts the accuracy and update-distance threshold to the InputSettings defaults.

    Declaration
    public void ResetConfiguration()
    Remarks

    Subject to the same application timing as Configure(float, float). Must be called from the main thread only.

    Extension Methods

    InputControlExtensions.CheckStateIsAtDefault(InputControl)
    InputControlExtensions.CheckStateIsAtDefault(InputControl, void*, void*)
    InputControlExtensions.CheckStateIsAtDefaultIgnoringNoise(InputControl)
    InputControlExtensions.CheckStateIsAtDefaultIgnoringNoise(InputControl, void*)
    InputControlExtensions.CompareState(InputControl, void*, void*)
    InputControlExtensions.CompareState(InputControl, void*, void*, void*)
    InputControlExtensions.CompareStateIgnoringNoise(InputControl, void*)
    InputControlExtensions.FindControlsRecursive<TControl>(InputControl, IList<TControl>, Func<TControl, bool>)
    InputControlExtensions.FindInParentChain<TControl>(InputControl)
    InputControlExtensions.GetStatePtrFromStateEvent(InputControl, InputEventPtr)
    InputControlExtensions.HasValueChangeInEvent(InputControl, InputEventPtr)
    InputControlExtensions.HasValueChangeInState(InputControl, void*)
    InputControlExtensions.IsActuated(InputControl, float)
    InputControlExtensions.IsPressed(InputControl, float)
    InputControlExtensions.ReadDefaultValueAsObject(InputControl)
    InputControlExtensions.ReadValueAsObject(InputControl)
    InputControlExtensions.ReadValueFromEventAsObject(InputControl, InputEventPtr)
    InputControlExtensions.ReadValueIntoBuffer(InputControl, void*, int)
    InputControlExtensions.ResetToDefaultStateInEvent(InputControl, InputEventPtr)
    InputControlExtensions.WriteValueFromObjectIntoEvent(InputControl, InputEventPtr, object)
    InputControlExtensions.WriteValueIntoEvent<TValue>(InputControl, TValue, InputEventPtr)
    InputControlExtensions.WriteValueIntoState(InputControl, void*)
    InputControlExtensions.WriteValueIntoState<TValue>(InputControl, TValue, void*)
    InputControlExtensions.CopyState(InputDevice, void*, int)
    InputControlExtensions.CopyState<TState>(InputDevice, out TState)
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)