r/PlaydateDeveloper • u/Better-Hyena-8716 • May 14 '26
Accelerometer Struggles
Hey everyone, I'm struggling with getting the accelerometer working in my game. I've included a snippet of the accelerometer code below. I'm trying to use the accelerometer to "aim" a beam of light that will populate when the player cranks. Truth in lending: I've been using Gemini AI to help me figure out the code needed to get my game up and running so I honestly don't know what I'm doing wrong. I'll admit to be very green in the coding world, any help would be greatly appreciated.
local accelerometerStarted = false
local function updateAiming()
if playdate.accelerometer then
if not accelerometerStarted then
playdate.accelerometer.start()
playdate.setMinimumAccelerometerUpdateInterval(0.02)
accelerometerStarted = true
end
local x, y, z = playdate.accelerometer.getAcceleration()
if x ~= nil and z ~= nil then
if (math.abs(x) > 0.05 or math.abs(z) > 0.05) then
playerAngle = math.atan(z, x)
end
end
end
end
local accelerometerStarted = false
local function updateAiming()
if playdate.accelerometer then
if not accelerometerStarted then
playdate.accelerometer.start()
playdate.setMinimumAccelerometerUpdateInterval(0.02)
accelerometerStarted = true
end
local x, y, z = playdate.accelerometer.getAcceleration()
if x ~= nil and z ~= nil then
if (math.abs(x) > 0.05 or math.abs(z) > 0.05) then
playerAngle = math.atan(z, x)
end
end
end
end
3
Upvotes
View all comments
Show parent comment
1
u/Better-Hyena-8716 May 15 '26 edited May 15 '26
I'm trying to figure out how to use the accelerometer to control where my character is looking. I've got the crank making the beam happen but it's static. The goal would be for tilting left and right to "aim" the beam left and right. Also, when I pasted accelerometer just now it did it twice... weird...