Currently, I'm using the ContextActionService
for my keyboard, mouse & gamepad button related events. However, for my steering, I want to use the position of the joystick on the controller.
I've found multiple ways to find the mouse's location, which I don't want to use at the moment, but no way to directly get the position of the gamepad joystick. If there is a way, I can't find where it says about it in the documentation. There's pressing it down, as in pressing it, but nothing about it's position
Any help would be appreciated.
If you want to get the joystick positions, you need to use ContextActionService to get the InputObject for the gamepad. The tilt of the joysticks is stored on the Position
property of the object. The Gamepad docs have a pretty good walkthrough for it.
Try something like this in a LocalScript:
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local gamepad = Enum.UserInputType.Gamepad1
if not UserInputService:GetGamepadConnected(gamepad) then
warn("No gamepad detected!")
return
end
local function handleInput(actionName : string, inputState : Enum.UserInputState, inputObject : InputObject)
if actionName == "MoveStick1" then
-- do something to handle the left joystick input
local moveVector : Vector3 = inputObject.Position
elseif actionName == "MoveStick2" then
-- do something to handle the right joystick input
local lookVector : Vector3 = inputObject.Position
end
end
ContextActionService:BindAction("MoveStick1", handleInput, false, Enum.KeyCode.Thumbstick1)
ContextActionService:BindAction("MoveStick2", handleInput, false, Enum.KeyCode.Thumbstick2)
To better see the values, I scaled the Position vector by 100, rounded the value, and put each dimension into a TextLabel.
In this example ...
Raw inputs from the joystick range from [-1, 1] on the X and Y axis. I don't know why the values are packed into a Vector3 and not a Vector2.