Retrieving move direction from the joystick?

Hi, I’m making a 2d game right now, and I’m trying to add mobile compatibility. I’m trying to get the move direction of joystick and get a Vector3 out of it.

I’ve done some research on this, and I can’t seem to figure out how to get it. Some people have said I should get the move direction, but I also want to get a SPEED attribute out of it. As in the .Magnitude of the Vector3 is equal to the speed. Some sources have said that the joystick can get you that, but I don’t know how. Especially because it’s IMPOSSIBLE to tests game on mobile with ease in studio.

Here’s the code that I wrote, this was me trying to get a Vector3 from the click position on the screen.

uis.TouchMoved:Connect(function(input, gpe)
    if not over then -- ignore this
	    lastmousepos = camera:ScreenPointToRay(input.Position.X, input.Position.Y).Direction + camera.CFrame.Position
    end
end)

If you’re using a humanoid, you can use the MoveDirection variable. If not, there might be a function in UserInputService or Player that allows you to get the movement direction. There wasn’t any when I last checked.

I recommend making your own controls if you aren’t using a Humanoid, since it will make gameplay more optimized, and controls will be unique for your game. You would probably want to use buttons instead of a joystick for a 2D game anyways.

You can use Humanoid.MoveDirection to know the direction the player is going in relation to the camera direction, but if you want to know the direction regardless of the player’s camera rotation you can use this

local Player = game:GetService("Players").LocalPlayer
local Camera = workspace.CurrentCamera

while task.wait() do
	local Character = Player.Character
	if Character then
		local function MoveDirection()
			local Humanoid = Character.Humanoid
			local RealMoveDirection = Humanoid.MoveDirection
			if RealMoveDirection ~= Vector3.zero then
				local LookDirection = Camera.CFrame.LookVector
				LookDirection = Vector3.new(LookDirection.X, 0, LookDirection.Z).Unit
				RealMoveDirection = Vector3.new(RealMoveDirection.X, 0, RealMoveDirection.Z).Unit
				local RelativeCFrame = CFrame.lookAt(Vector3.zero, LookDirection)
				local MoveDirection = RelativeCFrame:VectorToObjectSpace(RealMoveDirection)
				return MoveDirection
			else
				return Vector3.zero
			end
		end
		print(MoveDirection()) --Get this
	end
end

Does this also incorporate different speeds? As in if you moved your joystick less, the magnitude lowers?

My solution to this has always been by modifying the PlayerModule that comes in PlayerScripts.

You can use the :GetMoveVector() method in the controls module, it returns values in the range 0-1 depending on the amount of movement on the mobile joystick. Otherwise works identically to the MoveDirection property of the humanoid.

local controls = game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"):GetControls()

--//controls:GetMoveVector()
3 Likes

Could you explain how I would do this for the future people that may come to this thread?