Mobile movement input script not working?

I’m trying to make a control scheme for a helicopter to be used on mobile. I’m trying to make the helicopters movement based on the character movement (i.e. using WASD for computer, using the thumb dpad on mobile), but I can’t get the script to work on mobile; the helicopter won’t move. Could someone help me?

----everything in this code, including terms not mentioned, is defined
local w,a,s,d,up,dn
local inputS=game:getService("UserInputService")
inputS.TouchStarted:Connect(function(object)
	if object == Enum.KeyCode.DPadUp then
		w=true
	elseif object == Enum.KeyCode.DPadLeft then
		a=true	
	elseif object == Enum.KeyCode.DPadDown then
		s=true	
	elseif object == Enum.KeyCode.DPadRight then
		d=true			
	end
end)
inputS.TouchEnded:Connect(function(object)
	if object == Enum.KeyCode.DPadUp then
		w=false
	elseif object == Enum.KeyCode.DPadLeft then
		a=false
	elseif object == Enum.KeyCode.DPadDown then
		s=false	
	elseif object == Enum.KeyCode.DPadRight then
		d=false		
	end
end)

DPad directions are for gamepads, don’t think they work for mobile. Consider a normalised direction from InputObject.Delta.

1 Like

Hello, in my opinion you are not using correct KeyCodes for mobile. You should check out this Gamepad-Input article to make it work for mobile. :herb:

That’s not an opinion, though? Either they’re the correct ones or they’re not.

1 Like

Hi theres an easy way to do this to get the movement vector of mobile controls (& every control) just do:

local ControlModule = require(game:GetService("Players").LocalPlayer.PlayerScripts.PlayerModule.ControlModule)

while wait() do
      local Vector = ControlModule:GetMoveVector()
      print(Vector)
      if Vector == Vector3.new(0, 0, 0) then
          print("Idle")
      elseif Vector.z >= -1 and Vector.z < 0 then
          print("Forward")
      elseif Vector.z > 0 then
          print("Backward")
      elseif Vector.x >= -1 and Vector.x < 0 then
          print("Left")
      elseif Vector.x > 0 then
          print("Right")
      end
   end
end)
5 Likes

How would you do this for the inverse, detecting once movement has stopped?

When movement stops it will print idle- or do you wanna detect if a certain control has been stopped?

Assuming you are asking about it detecting if a certain control has stopped here ya go-

local ControlModule = require(game:GetService("Players").LocalPlayer.PlayerScripts.PlayerModule.ControlModule)

while wait() do
	local Vector = ControlModule:GetMoveVector()
	print(Vector)
	if Vector == Vector3.new(0, 0, 0) then
		print("Idle")
	end
	if Vector.z >= -1 and Vector.z < 0 then
		print("Forward")
	else
		print("Not Moving forward-")
	end
	if Vector.z > 0 then
		print("Backward")
	else
		print("Not Moving Backwards-")
	end
	if Vector.x >= -1 and Vector.x < 0 then
		print("Left")
	else
		print("Not Moving Left-")
	end
	if Vector.x > 0 then
		print("Right")
	else
		print("Not Moving Right-")
	end
end```
1 Like