Help With Accelerometer Controls

Hello and thanks for looking at this post. 

 

I am having trouble writing a smooth accelerometer control function. Right now, because it’s sensing for tilt greater than 0, it gives off a sudden stop/jerk when switching directions. I tried looking at some simple examples online but the results were not smooth at all and problematic. Does anyone have any suggestions how to avoid the jerkiness and keep smoothness when the character moves? 

 

Here is the code:

 

local function onTilt( event ) local u,v = player:getLinearVelocity() local tilt = event.yGravity local newDirection = 0 if math.abs(tilt) \> .1 then newDirection = tilt/math.abs(tilt) player:setLinearVelocity(-newDirection\*200,v) else player:setLinearVelocity(0,v) newDirection = 0 end direction = newDirection end

 

Thanks!!!