Earlier this month, I posted some code for mobile controls in my Bumper Cars game. Since then, the game has been rescripted and organized to cause less lag, fix a few major bugs, and make way for upcoming features. Included in this update, the mobile function in my script has been redone, and I would like some feedback from others on its efficiency:
local accelerate = false
local turn = false
local EnableGyro = coroutine.wrap(function()
while player do
local delta, cframe = UIS:GetDeviceRotation()
local degX = math.deg(cframe.LookVector.X)
if math.abs(degX) >= 10 and not turn then
turn = true
Device.input(tostring(degX/math.abs(degX)).."T", Enum.UserInputState.Begin)
Device.input(tostring(-degX/math.abs(degX)).."T", Enum.UserInputState.End)
elseif math.abs(degX) < 10 and turn then
turn = false
Device.input("1T", Enum.UserInputState.End)
Device.input("-1T", Enum.UserInputState.End)
end
local degZ = math.deg(cframe.LookVector.Z)
if math.abs(degZ)-10 >= 10 and not accelerate then
accelerate = true
Device.input(tostring(-degZ/math.abs(degZ)).."A", Enum.UserInputState.Begin)
Device.input(tostring(degZ/math.abs(degZ)).."A", Enum.UserInputState.End)
elseif math.abs(degZ)-10 < 10 and accelerate then
accelerate = false
Device.input("1A", Enum.UserInputState.End)
Device.input("-1A", Enum.UserInputState.End)
end
wait()
end
coroutine.yield()
end)
EnableGyro()
This function is contained in the module script and run on mobile devices. The Device.input
function fires a RemoteEvent
signal to the server, which then updates the variables in the car engine server scripts.