Converting old fly tool to mobile

Alright… So me and @lSteveRogersl recently released out first game. One major bug is that the fly tool we are using(Made by @Stickmasterluke) only works on PC since it relies on button presses(WASD) in order to move.

I am new to mobile game development and this is the first game Ive really had to take into consideration about it.

This code works good on PC.

game:GetService("UserInputService").InputBegan:Connect(function(code)
			if code.KeyCode==Enum.KeyCode.Space or code.UserInputType == Enum.UserInputType.Touch then					--Space bar
				fly()
				local torso=sp.Parent:FindFirstChild("Torso")
				if torso~=nil then
					torso.Velocity=momentum
				end
			elseif code.KeyCode==Enum.KeyCode.W or code.UserInputType == Enum.UserInputType.Touch then
				controls.forward=-1
			elseif code.KeyCode==Enum.KeyCode.A then
				controls.left=-1
			elseif code.KeyCode==Enum.KeyCode.S then
				controls.backward=1
			elseif code.KeyCode==Enum.KeyCode.D then
				controls.right=1
				spawn(function()
					
				end)
			end
		end)

I have already converted it from the older mouse.KeyDown event to UserInputService but I am having a hard time figuring out the function to replace the WASD. If anyone could help, I’d be appreciated!

1 Like

ContextActionService is good for interfacing with mobile controls on a basic level. You could probably just bind a new action to each key and set the mobile button generation argument to true

-- pseudocode!
for key in {--[[every key you want to use]]} do

ContextActionService:BindAction(--[[keyName]], function(name, state, input)
    -- we don't really need `name` or `input`
    if state == Enum.UserInputState.Begin then
        controls.--[[direction]] = 1
    end

-- this boolean is what creates the button!
end, true, key)

You will probably want to edit the button’s position or name, or anything else, in which case you can use ContextActionService:GetButton to get the gui object, or ContextActionService:SetTitle or similar to edit other parts of the button. I suggest you research this more from the dev hub though:

https://developer.roblox.com/en-us/api-reference/class/ContextActionService

3 Likes

Alright, I appreciate it, Ill be sure to check it out and see if I can make any sense of it!

1 Like

I’m pretty sure movement vector will work with this. (Works with all devices)

Wrote this code on my phone so it may not work but you should have an idea of how to use it.

local GetMoveVector = require(player:WaitForChild("PlayerScripts"):WaitForChild("ControlScript").MasterControl).GetMoveVector

local direction = GetMoveVector()
local X = direction.X
local Y = direction.Y
local Z = direction.Z

if X > 0 then
controls.right = X
elseif X < 0 then
controls.left = -X
else
controls.right = 0
controls.left = 0
end

if Z > 0 then
controls.forward = Z
elseif Z < 0 then
controls.backward = -Z
else
controls.forward = 0
controls.backward = 0
end

@12904

1 Like

You could try MouseButton1Down, which seems to work on mobile. You’ll just have to add this to the code:

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:connect (function()
    controls.foreward = -1
end)

Mobile devices don’t have mouses. Using PlayerMouse.Button1Down is somewhat of a hack in terms of getting click input to work on Roblox. The UserInputService has a large amount of mobile-based functions for your usage - in this case, you’d want TouchTap.


Based on this definition, a ‘hack’ doesn’t sound like much of a bad thing.

Using MouseButton1Down is probably best for features that work when clicked on both mobile and PC.

In this case, it’s referring to the first one, quick and inelegant. Using a mouse feature on mobile is bad and improper code. You should not be using mouse input functionality on touch devices, touch input functions exist for a reason.

Not really improper, if you’re aiming for a feature that will work on both PC and mobile. Programming both seems like a waste of time to me, unless I’m wrong and the touch input feature works differently? I would not know because my game is mainly supported for PC, I have not taken any time to make features work for mobile. If touch input reacts differently, I would like to know and I’ll consider recoding some features to work better for mobile.

I really don’t know what kind of point you’re trying to prove here because you’re hardly explaining anything other than your lack of familiarity with programming for mobile devices. You aren’t supposed to use computer input on mobile devices. I’ve said this multiple times. Using mouse input on a touch device is bad practice and doesn’t extract the full usefulness of touch-based connections.

If you want a feature that works on multiple devices, you tailor it to be compatible with multiple input types. That’s it. It’s improper. Mobile devices don’t have mouses and the mouse object doesn’t cover all use cases for mobile-based input.

It does, far differently. You can easily read documentation on UserInputService regarding touch functions to figure this out on your own. There is support for all types of touch-based input.

Various functionality specific to mobile devices.

I’m sorry that you don’t see the point in writing proper code but that doesn’t mean you try to pass bad practice off as genuine. Not only is it bad code, but it’s garbage for readability and compatibility for mobile-specific programming.

You could easily do your research on the Developer Hub before making a post here. I absolutely hate developing for mobile devices and barely do, but that doesn’t mean I use PC input when attempting to make features for mobile nor do I try and tell others to follow that paradigm.

The fact that you don’t develop for mobile doesn’t really give you the pass to say that using PC input is proper practice for mobile devices. There are functions designed specifically for working with mobile input in mind, which are the aforementioned.

Amidst all this though, keep in mind the topic of the thread in which OP is attempting to convert WASD keys for touch devices. Forward input is not enough in this case and you need to get the positioning of either a thumbstick or a swipe direction to determine where a user wants to travel.

Okay, I was very wrong. I have not programmed a mobile game, and my PC game works well enough on mobile.

I’ll try to use these. Thanks for the info.
@12904 this would probably work well for your problem, too.

1 Like