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

how to make a ray stop before it meets its set endpoint?

Asked by 5 years ago

i know the title isn't exactly the best, but i'll try to explain what i mean a bit better. so, say i have a starting point for my ray in one place, and the end point is behind a wall. the ray also draws a "laser", or a block that essentially visualizes the ray. what i don't want to happen is the laser to go through the wall, but instead stop when it hits the wall. in case i didn't explain it well enough in text form, check out this visual i drew up. this is the code i have so far, which draws the ray through anything without stopping. the part which deals with the size of the laser/ray is within the repeat block.

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

function DRAW()

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

        beam.Parent = workspace

        repeat
        wait()
        local point = game.Workspace.NinjaboySC.HumanoidRootPart
        local ray = Ray.new(tool.CFrame.p, (point.Position - tool.CFrame.p).unit * 300)


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


    until game.Workspace.NinjaboySC == nil

end

wait(3)
DRAW()

1 answer

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Use :FindPartOnRay with the ray that you've created to return any part and position of the hit.

local ray = Ray.new(tool.CFrame.p, (point.Position - tool.CFrame.p).unit * 300)

--The function also includes an ignorelist if you need it
local part, hitPos = workspace:FindPartOnRay(ray)

--part is the part it hit
--hitPos is a Vector3

It can also return the normal and material of the part it hit.

hitPos is the position you're looking for.

0
You can use this to: workspace:FindPartOnRayWithIgnoreList(Ray, Ray_Ignore). I put the client's character, camera and trails/lasers in Ray_Ignore hellmatic 1523 — 5y
Ad

Answer this question