Detecting Character Movement On Mobile Controls

Is there a way to detect forward, back, left and right movement on a mobile device?

Detecting character movement on a keyboard device is simple using PlayerActions:

game:GetService("ContextActionService"):BindAction("forward", OnCharacterMovementAction, false, Enum.PlayerActions.CharacterForward)

However, on a mobile device, the above code does not trigger when the user moves the virtual movement joystick (not sure if that is a bug or not).

3 Likes

This article might help

https://www.robloxdev.com/articles/Mobile-Controls

tl;dr find the mobile controls UI and bind to TouchSwipe.

This is the current best way to get movement input, regardless of platform:

https://devforum.roblox.com/t/best-input-detection-for-mobile-controls/48982/5

It should be noted that with the release of player scripts v2 soonish, this specific method will break and the way you get the input vector from the control script will be slightly different.

7 Likes

This is really helpful, thank you!

Would you happen to know what the values of this vector represent? On a keyboard, the values are integers -1, 0, 1 for x, y, z depending on the direction you are going. But for the mobile controls the values are real numbers over a wider number range.

1 Like

I think the Y value is not used.

The X and Z values are a 2D vector that represents the direction of the movement input. For keyboard, X and Z can only be -1, 0, or 1, but with the mobile thumbstick and gamepad there is analog control, so the vector components are not discrete. In this case, the magnitude of the vector is used for walkspeed control.

2 Likes