Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I edit this so all the raycasts come out of one part/location and spread out, shotgun-style?

Asked by 8 years ago

So all I did was take the raycasting from the wiki and edit it a bit, and tried to make it into a shotgun, I can but having a little trouble with what the title states.

local Tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local vExit = Tool:WaitForChild("Exit")

Tool.Equipped:connect(function(mouse)

    mouse.Button1Down:connect(function()
        local ray = Ray.new(Tool.Exit.CFrame.p, (mouse.Hit.p - Tool.Exit.CFrame.p).unit * 45)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)


        for i = 1, 10 do  --makes more than one part/beam brick
        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Medium stone grey")
        beam.FormFactor = "Custom"
        beam.Material = "Plastic"
        beam.Transparency = 0.825
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (Tool.Exit.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.05, 0.05, distance)
        beam.CFrame = CFrame.new(Tool.Exit.CFrame.p, position) * CFrame.new(0,0--[[ This is where I'm having trouble I can use math.random to replace the 0's but the bullets come out of nowhere, offset to the side, how can I fix that?]], -distance / 2)

        game:GetService("Debris"):AddItem(beam, 0.1)

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(math.random(3.5,7))
                end
            end
        end
    end)
end)

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

One solution would be to move the part that makes the ray under the for loop so it creates a new one each time, and set it to be offset by random values.

local Tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local vExit = Tool:WaitForChild("Exit")

Tool.Equipped:connect(function(mouse)

    mouse.Button1Down:connect(function()

        for i = 1, 10 do  --makes more than one part/beam brick
        local ray = Ray.new(Tool.Exit.CFrame.p, ((mouse.Hit.p + Vector3.new(math.random(1, 10), math.random(1,5), math.random(1, 10)) - Tool.Exit.CFrame.p).unit * 45)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Medium stone grey")
        beam.FormFactor = "Custom"
        beam.Material = "Plastic"
        beam.Transparency = 0.825
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (Tool.Exit.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.05, 0.05, distance)
        beam.CFrame = CFrame.new(Tool.Exit.CFrame.p, position) * CFrame.new(0,0--[[ This is where I'm having trouble I can use math.random to replace the 0's but the bullets come out of nowhere, offset to the side, how can I fix that?]], -distance / 2)

        game:GetService("Debris"):AddItem(beam, 0.1)

        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")

            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end

            if humanoid then
                humanoid:TakeDamage(math.random(3.5,7))
                end
            end
        end
    end)
end)

A problem with this, though, is that if the player shoots towards the sky, or anything that causes mouse.Hit.p to contain giant vector3 values, it will only fire one beam.

0
thats what i was thinking, but i couldnt put it into words koolkid8099 705 — 8y
0
Thanks for the help, apparently line 10 needed to be "local ray = Ray.new(Tool.Exit.Position," because I placed it at the end of the barrel. Evadable 65 — 8y
0
No problem. Pyrondon 2089 — 8y
Ad

Answer this question