PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

DeviceAccelerationChanged

The DeviceAccelerationChanged event fires when a user moves a device that has an accelerometer.

An accelerometer is a component found in most mobile devices that measures acceleration (change in speed).

To determine whether a user’s device has an accelerometer enabled, seeUserInputService/AccelerometerEnabled.

This event can be used to track the movement of a device that has an accelerometer. A sample usage includes moving the player character when a mobile device accelerates.

Additionally, this event can be used along with UserInputService/GetDeviceAcceleration to determine the current movement of a user’s device if the device has an accelerometer.

This event only fires locally - which means that only the player whose device moves can use the event and it will only work in a LocalScript.

Parameters

Name Type Default Description

An InputObject, with a InputObject/UserInputType|UserInputType of ’Accelerometer’, and InputObject/Position|Position that shows the force of gravity on each local device axis


Code Samples


Control Players Using the Accelerometer

This example uses the accelerometer to move the player character when a mobile device is accelerated. The character will will along the axis that the device was moved. This code can be seen in this uncopylocked place.

local UserInputService = game:GetService("UserInputService")
local sensitivity = .2
 
local player = game.Players.LocalPlayer
while not player.Character do wait() end
local humanoid = player.Character:WaitForChild("Humanoid")
 
local ready = true
local function ChangeAcceleration(acceleration)
    if ready then
   	 ready = false
   	 local accel = acceleration.Position
 
   	 if accel.Y >= sensitivity then
   		 humanoid.Jump = true
   	 end
 
   	 if accel.Z <= -sensitivity then
   		 humanoid:Move(Vector3.new(-1,0,0))
   	 end    
   	 if accel.Z >= sensitivity then
   		 humanoid:Move(Vector3.new(1,0,0))
   	 end    
   	 if accel.X <= -sensitivity then
   		 humanoid:Move(Vector3.new(0,0,1))
   	 end    
   	 if accel.X >= sensitivity then
   		 humanoid:Move(Vector3.new(0,0,-1))
   	 end    
   	 wait(1)
   	 ready = true
    end
End
UserInputService.DeviceAccelerationChanged:Connect(ChangeAcceleration)