I am working on a automatic firing gun for both mobile and pc. Works great on pc, but not very good for mobile. When I tap and hold on a point, bullets are shoot in the direction of that tap. However, when I move my character using the joystick, the bullets change direction and start firing towards the joystick. I was wondering if there was a way to ignore the mobile joystick or at least the position of the joystick.
Video of problem:
My gun uses UserInputService. One script manages the damage dealt to humanoids, while another, a local script, handles player tap positions and mouse controls.
Here is that local script:
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local origin1 = handle:WaitForChild("MuzzleFlash")
local event = tool:WaitForChild("RemoteEvent")
local event2 = tool:WaitForChild("RemoteEvent2")
local player = game.Players.LocalPlayer
local camera = game.Workspace.Camera
local hit, model, humanoid, lastTap2d, attchPartFolder
local usi = game:GetService("UserInputService")
local debris = game:GetService("Debris")
local equipped = false
local canfire = true
local holddown = false
local shootDelay = 1 / 3
local timePerBullet = 0.2
local RELOAD_TIME = 0.4
local rate = 1 / 35
local RANGE = 200
tool.Equipped:Connect(function()
equipped = true
end)
tool.Unequipped:Connect(function()
equipped = false
end)
local function raycast(mousePos, location)
local origin = player.Character:FindFirstChild("HumanoidRootPart").Position
local bulletorigin = origin
local direction = (mousePos - origin).Unit * RANGE
local params = RaycastParams.new()
params.FilterDescendantsInstances = {tool, player.Character or player.CharacterAdded:Wait(), attchPartFolder}
params.FilterType = Enum.RaycastFilterType.Exclude
local result = workspace:Raycast(origin, direction, params)
if result then
hit = result.Instance
else
hit = nil
end
event:FireServer(mousePos, usi, hit, location)
end
function getMousePos()
local ignore = {player.Character or nil, attchPartFolder}
local mouseLocation2d = usi:GetMouseLocation()
local unitray = camera:ScreenPointToRay(mouseLocation2d.X-math.random(0, 10) or mouseLocation2d.X+math.random(0, 10), mouseLocation2d.Y-math.random(30, 40), 0)
local ray = Ray.new(unitray.Origin, unitray.Direction * 200)
local target, position = workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)
return position
end
function getTapPos(touchPositions)
local ignore = {player.Character or nil, attchPartFolder}
local unitray = camera:ScreenPointToRay(lastTap2d.X, lastTap2d.Y, 0)
local ray = Ray.new(unitray.Origin, unitray.Direction * 200)
local target, position = workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)
return position
end
usi.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
local inputType = input.UserInputType
if input.UserInputType == Enum.UserInputType.MouseButton1 and equipped then
holddown = true
while holddown do
local fireCoroutine = coroutine.wrap(function()
local mousePos = getMousePos()
local mouseLocation2D = usi:GetMouseLocation()
print(mousePos)
raycast(mousePos, mouseLocation2D)
end)
fireCoroutine()
wait(0.09)
end
elseif input.KeyCode == Enum.KeyCode.ButtonR1 and equipped then
holddown = true
while holddown do
local fireCoroutine = coroutine.wrap(function()
local mousePos = getMousePos()
local mouseLocation2D = usi:GetMouseLocation()
raycast(mousePos, mouseLocation2D)
end)
fireCoroutine()
wait(0.09)
end
elseif input.UserInputType == Enum.UserInputType.Touch and equipped then
holddown = true
if canfire then
canfire = false
local initialTouchPos = input.Position
local mousePos
while holddown do
local fireCoroutine = coroutine.wrap(function()
local mouseLocation2D = usi:GetMouseLocation()
mousePos = getTapPos(input)
raycast(mousePos, mouseLocation2D)
end)
fireCoroutine()
wait(0.09)
end
end
else if not canfire then
return
end
end
end)
usi.InputEnded:Connect(function(input, gameProcessed)
if gameProcessed then return end
local inputType = input.UserInputType
if input.UserInputType == Enum.UserInputType.MouseButton1 and equipped then
holddown = false
elseif input.UserInputType == Enum.UserInputType.Touch and equipped then
holddown = false
canfire = true
end
end)
usi.TouchTap:Connect(function(positions, gameProcessed)
if gameProcessed then return end
lastTap2d = Vector2.new(positions[1].X, positions[1].Y)
end)
event2.OnClientEvent:Connect(function(player, mousepos, fadedelay)
local Ignore = {player.Character, tool}
attchPartFolder = workspace:FindFirstChild("UZIBullets_" .. player.Name)
if not attchPartFolder then
attchPartFolder = Instance.new("Folder", workspace)
attchPartFolder.Name = "UZIBullets_" .. player.Name
end
for _, part in ipairs(attchPartFolder:GetChildren()) do
table.insert(Ignore, part)
end
local ray = Ray.new(tool.Handle.CFrame.p, (mousepos - tool.Handle.CFrame.p).Unit * RANGE)
local part, position = workspace:FindPartOnRayWithIgnoreList(ray, Ignore, false, true)
local origin = player.Character:FindFirstChild("HumanoidRootPart").Position
local attchPart = Instance.new("Part", workspace)
attchPart.Name = "AttachmentPart"
attchPart.CanCollide = false
attchPart.CanTouch = false
attchPart.Anchored = true
attchPart.Locked = true
attchPart.Transparency = 0
attchPart.Material = Enum.Material.Neon
attchPart.BrickColor = BrickColor.new("White")
local distance
if hit and not hit:IsDescendantOf(player.Character) then
model = hit:FindFirstAncestorOfClass("Model")
if model then
humanoid = model:FindFirstChild("Humanoid")
end
end
if hit and not hit:IsDescendantOf(player.Character) and humanoid then
distance = (hit.Position - tool.Handle.CFrame.p).magnitude
else
distance = (position - tool.Handle.CFrame.p).magnitude
end
attchPart.Size = Vector3.new(0.45, 0.45, distance)
attchPart.CFrame = CFrame.new(tool.Handle.CFrame.p, mousepos) * CFrame.new(0, 0, -distance / 2)
attchPart.Parent = attchPartFolder
debris:AddItem(attchPart, fadedelay * 3)
wait(0.1)
local frames=fadedelay/rate
for frame=1,frames do
wait(0.02)
local percent=frame/frames
attchPart.Transparency=.5+(percent*.5)
end
wait(0.4)
attchPart:Remove()
end)
I would appreciate any assistance, thanks.