Mobile support raycast gun

i want about input for mobile. because now when i hold press on mobile and i work it’s will sometime go to finger postion and walking position. i want it stick at same place until i put finger put

> -- Constants
local tool = script.Parent
local COOLDOWN = tool:GetAttribute("Cooldown")
local RANGE = tool:GetAttribute("Range")
local DAMAGE = tool:GetAttribute("Damage")
-- Variables
local origin = tool:WaitForChild("Origin")
local handle = tool:WaitForChild("Handle")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gunEvent = script.Parent.GunEvent
local equipped = false
local mouseDown = false

-- Sounds
local Sound = {
	Fire = handle:WaitForChild("GunFire"),
	Equip = handle:WaitForChild("GunEquip"),
	UnEquip = handle:WaitForChild("GunUnEquip")
}

-- Equipping and unequipping
tool.Equipped:Connect(function()
	Sound.Equip:Play()
	equipped = true
end)

tool.Unequipped:Connect(function()
	Sound.UnEquip:Play()
	equipped = false
end)

-- Fire gun function
local function FireGun()
	local originPos = origin.Position
	local mousePos = mouse.Hit.Position
	Sound.Fire:Play()
	gunEvent:FireServer(originPos, mousePos, RANGE, DAMAGE)
	wait(COOLDOWN)
end

-- Input handling
mouse.Button1Down:Connect(function()
	if equipped then
		mouseDown = true
		while mouseDown do
			FireGun()
		end
	end
end)

mouse.Button1Up:Connect(function()
	mouseDown = false
end)
e
1 Like

Try using ContextActionService:

1 Like
local userinputservice = game:GetService("UserInputService")
local contextactionservice = game:GetService("ContextActionService")
local tool = script.Parent
local COOLDOWN = tool:GetAttribute("Cooldown")
local RANGE = tool:GetAttribute("Range")
local DAMAGE = tool:GetAttribute("Damage")
-- Variables
local origin = tool:WaitForChild("Origin")
local handle = tool:WaitForChild("Handle")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gunEvent = script.Parent.GunEvent
local equip = false
local debouce = true
-- Sounds
local Sound = {
	Fire = handle:WaitForChild("GunFire"),
	Equip = handle:WaitForChild("GunEquip"),
	UnEquip = handle:WaitForChild("GunUnEquip")
}

local function CheckDevice()
	if userinputservice.TouchEnabled then
		print("player is on")
	else
		print("player is off")
	end
end

CheckDevice()

local function FireGun()
	local originPos = origin.Position
	local mousePos = mouse.Hit.Position
	Sound.Fire:Play()
	gunEvent:FireServer(originPos, mousePos, RANGE, DAMAGE)
	wait(COOLDOWN)
end


local function touchcontrol(actionName, InputState, InputObject)
	if InputState == Enum.UserInputState.Change or Enum.UserInputState.Begin and equip then
		FireGun()
	elseif InputState == Enum.UserInputState.End then
		print("end")
	end
end

tool.Equipped:Connect(function()
	equip = true
end)

tool.Unequipped:Connect(function() -- It might be UnEquipped, I'm not sure.
	equip = false
end)

contextactionservice:BindAction("Touchcontrol", touchcontrol, false, Enum.UserInputType.Touch)

i try but it make my camera can rotate on mobile and it’s still change to my thumbstick finger position when i walk. i want it stick at same position with first touch input

For the shooting thing, do Camera.CFrame.Position + (Camera.CFrame.LookVector*1000) or Camera.Cframe:ToWorldSpace(CFrame.New(0,0,-1000).