KillerFrog11
KillerFrog11

Reputation: 55

How to get gamepad joystick position on Roblox

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.

Upvotes: 1

Views: 1370

Answers (2)

Red pants
Red pants

Reputation: 1

you can also use userInputService.InputChanged to get the 'Position' value of the input object

Upvotes: 0

Kylaaa
Kylaaa

Reputation: 7188

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.

enter image description here

In this example ...

  • the Left Joystick is in the lower right quadrant
  • the Right Joystick is roughly in the middle left

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.

Upvotes: 2

Related Questions