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)
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
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
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.
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)
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