Here is a resource I found cool by looking at this vehicle input module.
One aspect I like a lot about it is that it’s possible to set multiple key binds to one action so it’s possible to have mobile or controller compatibility, by converting a table of Enums into a dictionary of Enums.
Code excerpt:
local controls = {}
controls.GearDown = {
Enum.KeyCode.F,Enum.KeyCode.Q, Enum.KeyCode.ButtonX
}
for ci,control in pairs(controls) do -- converting enums to have readable indexes.
local new = {}
for i,keycode in pairs(control) do
local index = keycode.Value
new[index] = index
end
controls[ci] = new
end
Then you can compare the input object during input began EnumCode with the dictionary to see if it’s a valid input then do something with it.
Edit: However the issue I found is that the vehicle input module is quite too realistic for my usage with a button for handbrake and changing gears which I didn’t need and just wanted something similar to a vehicle seat so here is the simplified version:
Vehicle input module to mimic vehicle seat throttle and steer
local m = {} -- functions
local inputs = {} -- input states
local controls = {} -- keybinds
inputs.Throttle = 0; -- User input determining the power sent to the wheels
inputs.Steer = 0; -- User input determining the angle at which the wheels should be turned.
controls.Throttle = {
Enum.KeyCode.W,Enum.KeyCode.Up,Enum.KeyCode.ButtonR2,Enum.UserInputType.Touch
}
--brake = -1 for throttle
controls.Brake = {
Enum.KeyCode.S, Enum.KeyCode.Down, Enum.KeyCode.ButtonL2,Enum.UserInputType.Touch
}
controls.SteerLeft = {
Enum.KeyCode.A, Enum.KeyCode.Left, Enum.KeyCode.Thumbstick1,Enum.UserInputType.Touch
}
controls.SteerRight = {
Enum.KeyCode.D, Enum.KeyCode.Right, Enum.KeyCode.Thumbstick1,Enum.UserInputType.Touch
}
--[[
controls.Handbrake = {
Enum.KeyCode.Space, Enum.KeyCode.ButtonA
}
]]
for ci,control in pairs(controls) do -- converting enums to have readable indexes.
local new = {}
for i,keycode in pairs(control) do
local index = keycode.Value
new[index] = index
end
controls[ci] = new
end
function m.GetControls() -- return the list of user input controls
return controls
end
function m.GetInputs() -- return the state of user inputs
return inputs
end
function m.GetInput(io,gpe) -- inputObject, gameProcessedEvent
local state = io.UserInputState.Name -- setting enums to strings with '.Name' is much easier
local typ = io.UserInputType.Name
local pos = io.Position --pos shows the current position of an input (e,g, mouse.X/Y)
local delta = io.Delta -- delta shows how much has CHANGED from last input
local key = io.KeyCode.Name
local code = io.KeyCode.Value -- gets the KeyCode number value
local gamepad = typ:match("Gamepad") -- is it a gamepad?
pos = Vector3.new(
pos.X * math.abs(pos.X), -- account for deadzone from thumbstick input.
pos.Y * math.abs(pos.Y),
pos.Z * math.abs(pos.Z)
)
--pos.Magnitude <= 0.125 and Vector3.new() or pos -- fixed deadzone variant
if gpe and not key == "Thumbstick1" then return end -- thumbstick is a GPE, who wouldn't known?...
--print(state,typ,delta,pos,code,touch)
-- print("State",state)
-- print("IOType",typ)
-- print("POS:",pos)
-- print("DELTA:",delta)
-- print("KEY:",key)
-- print(controls.Throttle[key])
-- print("Gamepad:",gamepad)
if controls.Throttle[code] then -- Throttle
if key:match("Button") then
inputs.Throttle = pos.Z
else
inputs.Throttle = state == "Begin" and 1 or 0
end
-- print("Throttle",inputs.Throttle)
end
if controls.Brake[code] then -- Brake
if key:match("Button") then
inputs.Brake = pos.Z
else
inputs.Throttle = state == "Begin" and -1 or 0
end
-- print("Brake:",inputs.Brake)
end
if controls.SteerLeft[code] or controls.SteerRight[code] then
if key:match("Thumbstick1") then
inputs.Steer = pos.X * (state == "End" and 0 or 1)
else
inputs.Steer = 1 * (controls.SteerLeft[code] and -1 or 1) * (state == "End" and 0 or 1)
end
-- print("Steer",inputs.Steer,pos.X)
end
end
return m