Flight Script Mobile Support

so bassicly, i got a flight script that works all fine, follows mouse, moves forward when u press “w” etc. but i want to add a mobile support (like goes where camera looks for mobile and moves with the mobile thing) but i never worked about mobile support before, so i dont got any idea

script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://9005390563"
local PlayAnim = Humanoid:LoadAnimation(Anim)
local HumaoidRP = Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local Anim2 = Instance.new("Animation")
Anim2.AnimationId = "rbxassetid://9005566129"
local Track2 = Humanoid:LoadAnimation(Anim2)
local TapTime = .25
local Tapped = false
local Toggle = false
local DidIt = false
UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Space then
		if not Tapped then
			Tapped = true
			wait(TapTime)
			Tapped = false
		else
			if Toggle == false then
				Track2:Play()
				local Jump = Instance.new("BodyVelocity",HumaoidRP)
				Jump.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
				Jump.Velocity = Vector3.new(0,50,0)
				game.Debris:AddItem(Jump,.5)
				wait(.5)
				HumaoidRP.Anchored = true
				Toggle = true
			elseif Toggle == true then
				Toggle = false
				Track2:Stop()
				PlayAnim:Stop()
				HumaoidRP.Anchored = false
				local Children = HumaoidRP:GetChildren()
				for i, child in pairs(Children) do
					if child:IsA("BodyVelocity") then
						child:Destroy()
					end
				end
			end
		end
	end
end)
UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W then
		if Toggle == false then return end
		PlayAnim:Play()
		Track2:Stop()
		HumaoidRP.Anchored = false
		if HumaoidRP:FindFirstChildOfClass("BodyVelocity") then
			HumaoidRP:FindFirstChildOfClass("BodyVelocity"):Destroy()
		end
		local Forward = Instance.new("BodyVelocity",HumaoidRP)
		Forward.Name = "ForwardMovement"
		Forward.MaxForce = Vector3.new(10000,10000,10000)
		local Gyro = Instance.new("BodyGyro",HumaoidRP)
		Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
		Gyro.D = 100
		Gyro.P = 10000
    
		while Toggle == true do
			if not DidIt then
				DidIt = true
				Forward.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			end
			Forward.Velocity = Mouse.Hit.lookVector*150
			Gyro.CFrame = Mouse.Hit
			wait()
			
		end
	end
end)
UIS.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W then
		if Toggle == false then return end
		if HumaoidRP:FindFirstChild("ForwardMovement") then
			HumaoidRP.ForwardMovement.MaxForce = Vector3.new(10000,10000,10000)
			wait(0.125)
			HumaoidRP.ForwardMovement.MaxForce = Vector3.new(8000,8000,8000)
			wait(0.125)
			HumaoidRP.ForwardMovement.MaxForce = Vector3.new(6000,6000,6000)
			wait(0.125)
			HumaoidRP.ForwardMovement.MaxForce = Vector3.new(4000,4000,4000)
			wait(0.25)
			HumaoidRP.ForwardMovement:Destroy()
			HumaoidRP.Anchored = true
			PlayAnim:Stop()
			Track2:Play()
			DidIt = false
			if HumaoidRP:FindFirstChildOfClass("BodyGyro") then
				HumaoidRP:FindFirstChildOfClass("BodyGyro"):Destroy()
			end
		end
	end
end)
UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.S then
		if Toggle == false then return end
		HumaoidRP.Anchored = false
		if HumaoidRP:FindFirstChildOfClass("BodyVelocity") then
			HumaoidRP:FindFirstChildOfClass("BodyVelocity"):Destroy()
		end
		local Back = Instance.new("BodyVelocity",HumaoidRP)
		Back.Name = "BackMovement"
		Back.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		local Gyro = Instance.new("BodyGyro",HumaoidRP)
		Gyro.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
		Gyro.D = 100
		Gyro.P = 10000
		while Toggle == true do
			Back.Velocity = Mouse.Hit.lookVector*-100
			Gyro.CFrame = Mouse.Hit
			wait()
		end
	end
end)
UIS.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.S then
		if Toggle == false then return end
		if HumaoidRP:FindFirstChild("BackMovement") then
			HumaoidRP.BackMovement:Destroy()
			HumaoidRP.Anchored = true
			if HumaoidRP:FindFirstChildOfClass("BodyGyro") then
				HumaoidRP:FindFirstChildOfClass("BodyGyro"):Destroy()
			end
		end
	end
end)


1 Like

I would recommend changing how your script works. As it stands you have duplicated UIS.InputBegan and UIS.InputEnded event bindings, which is inefficient. You should have it structured like so:

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Space then
		-- then stuff
	elseif Input.KeyCode == Enum.KeyCode.W then
		-- stuff
	elseif Input.KeyCode == Enum.KeyCode.S then
		-- stuff
	end
end

UIS.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Space then
		-- then end stuff
	elseif Input.KeyCode == Enum.KeyCode.W then
		-- end stuff
	elseif Input.KeyCode == Enum.KeyCode.S then
		-- end stuff
	end
end

However, if you want to create buttons for input on mobile devices, then you will need to use ContextActionService to detect input. The following acrticle explains how:
ContextActionService

1 Like