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

Raycast stop a ray after a distance?

Asked by
Prioxis 673 Moderation Voter
9 years ago

So I made a gun following the wiki http://wiki.roblox.com/index.php?title=Making_a_ray-casting_laser_gun

but when I shoot I can fire all the way from 1 side of the map to the other how do I implement it to where the ray stops after a certain distance?

My script

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
    print("Tool Equipped!")


mouse.Button1Down:connect(function()
    print("Mouse Pressed!")
    local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)  

    local beam = Instance.new("Part", workspace)
    beam.BrickColor = BrickColor.new("Bright green")
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0.25
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local distance = (tool.Handle.CFrame.p - position).magnitude
    beam.Size = Vector3.new(0.3, 0.3, distance)
    beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0,0, -distance / 2)

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



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

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

            if humanoid then
                humanoid:TakeDamage(script.Parent.Customization.Damage.Value)
            end     

    end


    end)
end)


1 answer

Log in to vote
1
Answered by 9 years ago

By definition, a ray cannot be stopped. A ray is a line segment, starting at one point and going infinitely in the specified direction. You can however, check how far away the part is.

local tool = script.Parent
local MaxRayDistance = 500
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
    print("Tool Equipped!")


mouse.Button1Down:connect(function()
    print("Mouse Pressed!")
    local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)  

    if (position - tool.Handle.CFrame.p).magnitude <= MaxRayDistance then
    local beam = Instance.new("Part", workspace)
    beam.BrickColor = BrickColor.new("Bright green")
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0.25
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local distance = (tool.Handle.CFrame.p - position).magnitude
    beam.Size = Vector3.new(0.3, 0.3, distance)
    beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0,0, -distance / 2)

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



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

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

            if humanoid then
                humanoid:TakeDamage(script.Parent.Customization.Damage.Value)
            end     

    end
end

    end)
end)



Should work. If not, let me know.

0
This doesn't work but I found a way around it line 11 you just change the number you multiply by to say 25 it makes it not go as far thank you though Prioxis 673 — 9y
0
What? Why the heck does that work? Alright, that's something to look at. TickerOfTime 70 — 9y
0
because all you did was created a variable and throw in a random if statement you pretty much did nothing Prioxis 673 — 9y
0
Wait lol. I forgot the `.magnitude`. I was asking how your changing numbers worked. TickerOfTime 70 — 9y
Ad

Answer this question