Getting moveDirection on mobile phone without Humanoid.Movedirection

I am trying to make my game mobile compatible and i got stuck at this point:


Since the player is sat down trying humanoid.MoveDirection would not work and i don’t know how to get the movement direction from the touchpad joystick thingy

According to the Gamepad Input article,

Most gamepads also support analog controls. To detect input from these, use the InputChanged event and detect the position of the input’s axis via InputObject.Position . The thumbstick’s position will always be on the X and Y axes between the values of -1 and 1, while the trigger buttons will only have values between 0 and 1 on the Z axis (0 at its starting position; 1 when fully pressed).

we can use this piece of code to get the thumbstick’s position

local UserInputService = game:GetService("UserInputService")
 
UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			print(input.Position.X, input.Position.Y)
		end
	end
end)

furthermore, since Humanoid.MoveDirection describes the direction a Humanoid is walking in, as a unit vector along the X/Z axis we can translate the input.Position into 3D space by doing

UserInputService.InputChanged:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			local MoveDirection = Vector3.new(input.Position.X, 0, input.Position.Y)
			MoveDirection = MoveDirection.Magnitude ~= 0 and MoveDirection.Unit or MoveDirection
			print(MoveDirection)
		end
	end
end)

On the 5th line, we’re normalizing the vector and making sure that its magnitude is exactly 1 or 0 as I’m not sure whether input.Position is normalized or not already (probably not).

4 Likes

ok gonna try that later :shallow_pan_of_food:

I did this recently on the client for a all device vehicle, it returns a vector3 of where they are moving, and I believe on joysticks it will return lesser values if it is pushed less, so it may help you:

local Plr = game.Players.LocalPlayer
while true do
   local MoveVector = require(Plr:WaitForChild("PlayerScripts").PlayerModule:WaitForChild("ControlModule")):GetMoveVector()
   print(MoveVector)
   wait()
end
1 Like

This still does not seem to work with the joystick for some reason
maybe it’s my script though, and the fact that it use inputbegan

No it returns 0,0,0 if i print it

Is your script a local script? AFAIK This won’t work on server scripts.

Yeah it’s a local script, and it does work with keys but not with gamepad.

strange, works perfectly fine for me using the mobile emulator, keyboard and mouse, and xbox controller