I am working on a automatic gun using raycasting. When I hold down my mouse to shoot repeatedly in the same spot, it appears that the mouse position changes with each bullet created. This should not be happening because the cursor is in the same position at all times. The bullet will keep moving upwards until it ends up in front of the camera. I am struggling to know where this is coming from and would appreciate some assistance.
Heres a video of the problem:
And here is the local script inside of the weapon which deals with getting the mouse position:
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()
-- ignore if mousepos is on bullet
params.FilterDescendantsInstances = {tool, player.Character or player.CharacterAdded:Wait(), attchPartFolder}
params.FilterType = Enum.RaycastFilterType.Exclude
local result = workspace:Raycast(origin, direction, params)
print(result)
if result then
hit = result.Instance
else
hit = nil
end
event:FireServer(mousePos, usi, hit, location)
end
function getMousePos()
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:FindPartOnRay(ray, player.Character or nil)
return position
end
function getTapPos(touchPositions)
local unitray = camera:ScreenPointToRay(lastTap2d.X, lastTap2d.Y, 0)
local ray = Ray.new(unitray.Origin, unitray.Direction * 200)
local target, position = workspace:FindPartOnRay(ray, player.Character or nil)
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(mouseLocation2D)
raycast(mousePos, mouseLocation2D)
end)
fireCoroutine()
wait(0.08)
end
elseif input.KeyCode == Enum.KeyCode.ButtonR1 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.08)
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
end
end)
usi.TouchTap:Connect(function(positions, processed)
if processed then return end
lastTap2d = Vector2.new(positions[1].X, positions[1].Y)
end)
usi.TouchLongPress:Connect(function(positions, processed)
if processed then return end
if equipped then
holddown = true
while holddown do
local fireCoroutine = coroutine.wrap(function()
local mousePos = getTapPos(positions, processed)
raycast(mousePos, lastTap2d)
wait(0.1)
end)
fireCoroutine()
wait(0.1)
end
end
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
print(mousepos)
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)
Thanks.