MobileGyroscopeService

Uses the gravity sensor of the mobile device. It can be called when the gravity sensor function is supported in the mobile device and the user device.

# Methods

[{"index":"Vector3 GetAttitudeEuler() (ClientOnly)","0":"Returns the device's angle of rotation as Euler angles."}]

[{"index":"Vector3 GetGravity() (ClientOnly)","0":"Returns the gravity acceleration vector that was expressed as the reference coordinate of the device."}]

[{"index":"Vector3 GetRotationRate() (ClientOnly)","0":"The rotation speed value that the gravity sensor measured."}]

[{"index":"Vector3 GetRotationRateUnbiased() (ClientOnly)","0":"The non-biased rotation speed value that the gravity sensor measured."}]

[{"index":"float GetUpdateInterval() (ClientOnly)","0":"A gravity sensor measurement cycle. The cycle's unit is 'second,' and the default value is 0.2 seconds."}]

[{"index":"Vector3 GetUserAcceleration() (ClientOnly)","0":"The acceleration value that was delivered to the device."}]

[{"index":"boolean IsEnabled() (ClientOnly)","0":"Returns whether the gravity sensor is currently measuring."}]

[{"index":"boolean IsHWSupported() (ClientOnly)","0":"Returns whether the device supports a gravity sensor function."}]

[{"index":"void SetUpdateInterval(float interval) (ClientOnly)","0":"Sets the gravity sensor measurement cycle. The unit is in seconds, and the default value is 0."}]

[{"index":"boolean StartAndWait() (ClientOnly) (Yield)","0":"Starts to measure the gravity sensor. Returns false upon measurement failure."}]

[{"index":"boolean StopAndWait(boolean resetData) (ClientOnly) (Yield)","0":"Ends measuring the gravity sensor. Delivers true value to resetData to initialize the last measurement value. Returns false upon ending failure."}]

##### inherited from Service:

[{"index":"EventHandlerBase ConnectEvent(string key, IScriptFunction eventHandler)","0":"Links events. When a key-type event occurs in this service, a handler is notified. The handler receives event parameters."}]

[{"index":"EventHandlerBase ConnectEvent(Type eventType, IScriptFunction eventHandler)","0":"Links events. This service notifies a handler when an eventType event occurs. The handler receives event parameters."}]

[{"index":"boolean DisconnectEvent(string key, EventHandlerBase eventHandler)","0":"Disconnects the event with the key-type event and the handler."}]

[{"index":"boolean DisconnectEvent(Type eventType, EventHandlerBase eventHandler)","0":"Disconnects the eventType event from its handler."}]

[{"index":"void SendEvent(EventType sendEvent)","0":"Generates an event."}]

# Examples

This example utilizes the gyroscope to move a ball-shaped Entity.
    
    
    @Component
    script GyroscopeForceController extends Component
        property number ForcePower = 100
    
        @ExecSpace("ClientOnly")
        method void OnBeginPlay()
        if _MobileGyroscopeService:IsHWSupported() then
        	local enabled = _MobileGyroscopeService:StartAndWait()
        	if enabled then
        		 _UIToast:ShowMessage("Gyroscope Started!")
        	else
        		log_error("Failed to start gyroscope")
        	end
        end
        end
    
        method void OnUpdate(number delta)
        if _MobileGyroscopeService:IsEnabled() then
        	-- Calculate the device's tilt value.
        	local rotRate = _MobileGyroscopeService:GetRotationRate()
        	local angularVelocity = Vector2(rotRate.x, rotRate.y)
        	-- Apply the device's instantaneous rotation speed as the force.
        	self:Impact(angularVelocity)
        end
        end
    
        @ExecSpace("ServerOnly")
        method void Impact(Vector2 dir)
        if dir.x > 0.2 or dir.y > 0.2 then
        	self.BallEntityRef.PhysicsRigidbodyComponent:ApplyForce(dir * self.ForcePower)   
        end
        end
    end
    

# SeeAlso

  * PhysicsRigidbodyComponent
  * [Using the Gravity Sensor of Mobile Devices](/docs?postId=831)