Mobile Car Tilt Controls

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.

1 Like

This is really quite unperformant. You’re utlizing lots of if statements instead of planning out a smart way of handling it. Try using https://draw.io to make an efficient system that works well without more than a couple if statements. If you have repeating code why not bundle.

For instance:
Instead of Up = Boolean, Down = boolean have UpDown = Boolean (True = up, False = down).
You also have a lot of UserInputState’s with .End and .Begin being repeated and repeated. This could be condensed to Enum.userInputState[value] or to a seperate function so it’s not repeating constantly.

4 Likes

Thanks, the code has already been updated since this post was made… (actually everything is in the process of being rewritten now anyway, so this along with a few bugs which have been reported will be fixed on the next update)

Nevertheless it is still a bit messy and does need fixing up. I’ll make sure to prioritize that more, thanks for the suggestion.

2 Likes

I highly recommend you do not shadow the UserInputService name. I’d recommend making UIS UserInputService and naming your module appropriately, such as InputModule or something similar.

1 Like

As I said before, the code has been updated since this post was made. The module name has already been changed.

I will update this post tomorrow (if possible) when the next update is published.

Edit: The update will be posted here, I will still fix this post once it is done.

1 Like

Awesome, looking forward to it.

1 Like