How to use gyro from mobile?

Hello everyone, could someone help me to use the cell phone screen rotation function, for example when I rotate my cell phone to the left the character will move to the left

Another example

You can use UserInputService:GetDeviceRotation()

Get the rotation of the phone → depending on what you want just ignore the Y and Z value and cframe the character left or right according to the X rotation.

API has an example here:

1 Like

Here’s an example script that rotates a cell phone screen in Roblox:

local Screen = script.Parent
local ScreenGui = Screen:WaitForChild("ScreenGui")

local function RotateScreen()
    if ScreenGui.Rotation == Enum.RotationType.Degree0 then
        ScreenGui.Rotation = Enum.RotationType.Degree90
    elseif ScreenGui.Rotation == Enum.RotationType.Degree90 then
        ScreenGui.Rotation = Enum.RotationType.Degree180
    elseif ScreenGui.Rotation == Enum.RotationType.Degree180 then
        ScreenGui.Rotation = Enum.RotationType.Degree270
    elseif ScreenGui.Rotation == Enum.RotationType.Degree270 then
        ScreenGui.Rotation = Enum.RotationType.Degree0
    end
end

Screen.TouchEnded:Connect(RotateScreen)

In this script, the cell phone screen is represented by a part called “Screen” with a child ScreenGui object. The RotateScreen function is called when the TouchEnded event fires on the “Screen” part. This function checks the current rotation of the ScreenGui and sets it to the next rotation in a sequence of 90-degree rotations (0 degrees, 90 degrees, 180 degrees, 270 degrees).

Note that this script only handles one screen rotation sequence. If you want to support more complex screen rotations (e.g., with different rotation sequences for different types of screens), you may need to modify the RotateScreen function or use additional logic to determine the rotation sequence to use for a given screen.

2 Likes

I tried to use this API example, but when I rotate the cell phone screen it doesn’t print another CFrame, I do not know what to do :frowning:

local UserInputService = game:GetService("UserInputService")

local gyroEnabled = UserInputService.GyroscopeEnabled

if gyroEnabled then
      local _inputObj, cframe = UserInputService:GetDeviceRotation()
      while true do
       wait(0.1)
       print("CFrame: {", cframe, "}")
      end
else
   print("Cannot get device rotation because device does not have an enabled gyroscope!")
end

Try to check if the game is loaded on top of all

game.Loaded:Wait()

Pou (the example shown) doesn’t use Gyroscope sensor, instead it uses the Accelerometer. Here is an modified example shown in Documentation

local UserInputService = game:GetService("UserInputService")

--Ball should exist in workspace
local Ball = workspace.Ball --You can change it to another part

local RealGravity = Instance.new("BodyForce") --Force based on device gravity/accelerometer
RealGravity.Parent = Ball

local AntiGravity = Instance.new("BodyForce") --Zero game gravity
AntiGravity.force = Vector3.new(0,workspace.Gravity*Ball:GetMass(),0)
AntiGravity.Parent = Ball

local function MoveBall(gravity)
    RealGravity.force = gravity.Position * workspace.Gravity * Ball:GetMass()
end

if UserInputService.AccelerometerEnabled then 
    UserInputService.DeviceGravityChanged:Connect(MoveBall)
end
--You can try creating a ball and trapping it in a box (or else it would probably escape to the void)

Edit: The script needs to be client-sided

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.