PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

GetDeviceGravity

This function returns an InputObject describing the device’s current gravity vector.

The gravity vector is determined by the device’s orientation relative to the real-world force of gravity. For instance, if a device is perfectly upright (portrait), the gravity vector is DataType/Vector3|Vector3.new(0, 0, -9.18). If the left side of the device is pointing down, the vector is Vector3.new(9.81, 0, 0). Finally, if the back of the device is pointing down, the vector is Vector3.new(0, -9.81, 0).

This function might be used to enable the user’s device to impact or control gravity within the game or move in-game objects such as a ball.

Gravity is only tracked for players using a device with an enabled gyroscope - such as a mobile device.

To check if a user’s device has an enabled gyroscope, check the value of UserInputService/GyroscopeEnabled. If the device has an enabled gyroscope, you can also use the UserInputService/DeviceGravityChanged event to track when force of gravity on the user’s device changes.

As UserInputService is client-side only, this function can only be used in a LocalScript.

Returns

Return Type Summary

An InputObject that describes the device’s current gravity vector


Code Samples


Moving Objects with the Gyroscope

Using the Gyroscope gives us the down direction for the player’s device. We can use this to move objects in the game world. This example implements a level where the bubble will move along the Z axis depending on the device’s current gryoscope position in Z.

An uncopylocked version of this example can be seen here.