Scripting Support | Lazer Resize

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)

Intro to Raycasting

I had also attempted using raycasting. But when I did research on that I coundn’t figure out why it wouldnt work.

Can you show the code you created that used raycasting?

Sure. Give me a moment as I had deleted it before. (So ill have to remake it)

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.

Could you show an example of this? (sorry, im new to raycasting as i started yesterday)

RaycastExample.rbxl (23.4 KB)

Script from that place:

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
2 Likes