How did they make this laser beam

How did they make this laser beam? It looks good so I tried to remake it my recreation it doesn’t even work. Can you help me figure out what’s wrong?

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local rs = game:GetService('RunService')

task.wait(3)

local lasergun = workspace:WaitForChild('lasergun'):Clone()
lasergun.Parent = camera

local debris = game:GetService('Debris')

rs.RenderStepped:Connect(function(dt)
	lasergun:SetPrimaryPartCFrame(camera.CFrame)
	
	local rayorigin = lasergun.tip.Position
	local raydirection = lasergun.tip.CFrame.LookVector * 100
	local param = RaycastParams.new()

	local result = workspace:Raycast(rayorigin, raydirection, param)
	
	if result then
		local part0 = Instance.new('Attachment')
		part0.Parent = lasergun.tip
		part0.Position = result.Position - lasergun.tip.Position
		local part1 = Instance.new('Attachment')
		part1.Parent = lasergun.tip
		part1.Position = (result.Position - lasergun.tip.Position) + Vector3.new(.1,0,.1)

		local trail = workspace.trailpart.Trail:Clone()
		trail.Parent = lasergun.tip
		trail.Attachment0 = part0
		trail.Attachment1 = part1
		
		debris:AddItem(part0, .1)
		debris:AddItem(part1, .1)
		debris:AddItem(trail, .1)
	end
	
end)


it places some sort of part where cursor is and gives it a trail

1 Like

How would I get the parts position to constantly update to where the raycast is without using RunService?

Why would you not want to use RunService? I’m sure a modern computer can handle a single raycast 30-60 times a second just fine.

I want to create a part if there’s a result and then use RunService to update the parts position but I don’t know how to do that without making a part every time theres a raycast which results in me making a bunch of parts that I don’t need

So you haven’t tried creating the part once outside the RunService connection, and then only updating the CFrame in the connection?

Would I shoot the ray inside the RunService connection and then check if there’s a result outside of the connection and then make another RunService connection to update the part’s position? I want to only create the part if there’s a result but I don’t know if you can check outside of the connection

No, do it in the same connection, like this:

local laserHit = ... -- The part
local barrelCFrame = ... -- The front of the barrel, pointing forwards

RunService.Heartbeat:Connect(function()
	-- Cast ray
	local result = workspace:Raycast(barrelCFrame.Position, barrelCFrame.LookVector)
	
	-- Update part position
	laserHit.Position = result.Position
end)

You can change the barrelCFrame variable to be the laser attachment’s CFrame instead if you want it to be slightly more realistic.

1 Like

That worked but my trail keeps moving back towards the barrel whenever I stop moving around

Video of issue

local lasergun = workspace:WaitForChild('lasergun'):Clone()
lasergun.Parent = camera


rs.RenderStepped:Connect(function(dt)
	lasergun:SetPrimaryPartCFrame(camera.CFrame)
	
	local rayorigin = lasergun.tip.Position
	local raydirection = lasergun.tip.CFrame.LookVector * 300

	local result = workspace:Raycast(rayorigin, raydirection)
	
	if result then
		local trailpart = workspace.trailpart
		trailpart.Position = result.Position
	end
	
end)

I just had to turn off cancollide for the part with the trail in it, thanks

why would you need to raycast? you can create part at cursor’s position, it will be the same as raycasting

If you want to optimize, you can instead of raycasting every frame, you can raycast 10 times per second and lerp cursor, this will look similar

I want the laser to be facing where the gun is facing rather than where the mouse is, here is a video of it

1 Like

Can you explain lerping to me? That’s something else I’ve been wanting to learn how to use. If you don’t want to, please send an article/devforum thread I can read

if it’s first person, then it’s the same

It’s simple math

local function LERP(a,b,t)
    return a + (b - a) * t
end

for i = 0, 1, 0.1 do
  -- code
  local pos = LERP(start,finish,i)
end

Lerping is using math to find number between two numbers, for example 5 > 10 where t = 0.5 is 7.5

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.