Hi, I’m releasing my deadzone filtering module.
This can, for example, be used to add a deadzone to an analog input. The thumbsticks on a game controller, or UI thumbsticks on a phone etc. The module is meant for gamepads but it is not limited to these. It takes both Vector3 inputs (from a controller, for example) and standalone number values.
You can use the module for any value range you want, it is not limited to the standard [-1, 1] range. E.g. [0, 1] works too. The deadzone itself is customizable as well.
Modulescript code:
local module = {}
function module:ValueAfterDeadZone(ZoneCenter, MaxRangeEachDirection, DeadZoneRangeEachDirection, Value)
local min = ZoneCenter + DeadZoneRangeEachDirection
local max = ZoneCenter + MaxRangeEachDirection
if (Value > ZoneCenter) and (Value > (ZoneCenter + DeadZoneRangeEachDirection)) then
return ((Value - min) / (max - min)) * MaxRangeEachDirection + ZoneCenter
elseif (Value < ZoneCenter) and (Value < (ZoneCenter - DeadZoneRangeEachDirection)) then
return ZoneCenter - (((((ZoneCenter - (Value - ZoneCenter)) - min) / (max - min)) * MaxRangeEachDirection + ZoneCenter) - ZoneCenter)
end
return ZoneCenter
end
function module:Vector3AfterDeadZone(ZoneCenter, MaxRangeEachDirection, DeadZoneRangeEachDirection, Vector3Value)
return Vector3.new(module:ValueAfterDeadZone(ZoneCenter, MaxRangeEachDirection, DeadZoneRangeEachDirection, Vector3Value.X), module:ValueAfterDeadZone(ZoneCenter, MaxRangeEachDirection, DeadZoneRangeEachDirection, Vector3Value.Y), module:ValueAfterDeadZone(ZoneCenter, MaxRangeEachDirection, DeadZoneRangeEachDirection, Vector3Value.Z))
end
return module
Example usage (filtering right analog stick movement):
local UserInputService = game:GetService("UserInputService")
local DeadZoneModule = require(game.ReplicatedStorage.DeadZoneProcessor)
local function inputChange(input)
if input.UserInputType == Enum.UserInputType.Gamepad1 and input.KeyCode == Enum.KeyCode.Thumbstick2 then
local newGamepadPos = DeadZoneModule:Vector3AfterDeadZone(0, 1, 0.15, input.Position)
print("Original position:", input.Position)
print("Position after deadzone:", newGamepadPos)
end
end
UserInputService.InputChanged:Connect(inputChange)