Hi! I’m trying to make a laser (part) that resizes when something goes in-between the laser, so it cuts off. I’ve tried using beams and trails but they don’t work, nor do they function correctly. Please help!
Ex: Lazer is 50 studs long, something goes in-between it halfway, lazer would resize to 25 studs (or based on that parts sized whatever is convenient enough)
You raycast from the source of the laser out in the direction, and it should return among other data the position of the first thing that blocks the laser.
local params = RaycastParams.new()
local emitter = script.Parent.Emitter
local beam = script.Parent.Beam
local range = 100
params.FilterDescendantsInstances = {emitter, beam}
params.FilterType = Enum.RaycastFilterType.Blacklist
local function fire()
local results = workspace:Raycast(emitter.Position, emitter.CFrame.LookVector * range, params)
-- Analyze results
if (results == nil) then
print("The laser didn't hit anything.")
else
print("The laser hit a part:", results.Instance)
print("Material of part:", results.Material)
print("Normal vector:", results.Normal)
end
-- Beam
local p = results and results.Position or (emitter.CFrame * CFrame.new(0, 0, -range)).Position
beam.Size = Vector3.new(0.5, 0.5, (emitter.Position - p).Magnitude)
beam.CFrame = CFrame.new((emitter.Position + p) / 2, p)
for i = 0.5, 1, 0.1 do
beam.Transparency = i
wait()
end
end
while (true) do
wait(1)
fire()
end