Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How would I make this Raycast gun shoot in the air?

Asked by 12 days ago

So I made this gun script. It works, but only when I point my mouse to a surface and shoot. I want it to shoot when it points to the sky also.

The script:

tool.Activated:Connect(function()
    firing = true
    while firing == true do
        local ray = Ray.new(script.Parent.barrelpos1.Position, (mouse.Hit.p - script.Parent.barrelpos1.Position).Unit * 100)
        local raypart, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {char}, false, true)

        local Dist = (mouse.Hit.p - script.Parent.barrelpos1.Position).magnitude

        local Lazer = Instance.new("Part")
        Lazer.Parent = game.Workspace
        Lazer.Anchored = true
        Lazer.CanCollide = false
        Lazer.Size = Vector3.new(0.1, 0.1, Dist)
        Lazer.CFrame = CFrame.new(script.Parent.barrelpos1.Position,mouse.Hit.p + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))) * CFrame.new(0, 0, -Dist/2)
        debris:AddItem(Lazer, 0.1)
        wait(0.3)
    end
end)

Any help would be appreciated.

1 answer

Log in to vote
0
Answered by 12 days ago

Try replacing mouse.Hit.p in line 07 with position from line 05. Because when the mouse is pointing towards the sky, there is no mouse.Hit, but there may very well still be a position since the ray will always have an end, even if there is no target.

tool.Activated:Connect(function()
    firing = true
    while firing == true do
        local ray = Ray.new(script.Parent.barrelpos1.Position, (mouse.Hit.p - script.Parent.barrelpos1.Position).Unit * 100)
        local raypart, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {char}, false, true)

        local Dist = (position - script.Parent.barrelpos1.Position).magnitude

        local Lazer = Instance.new("Part")
        Lazer.Parent = game.Workspace
        Lazer.Anchored = true
        Lazer.CanCollide = false
        Lazer.Size = Vector3.new(0.1, 0.1, Dist)
        Lazer.CFrame = CFrame.new(script.Parent.barrelpos1.Position,mouse.Hit.p + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))) * CFrame.new(0, 0, -Dist/2)
        debris:AddItem(Lazer, 0.1)
        wait(0.3)
    end
end)
Ad

Answer this question