Raycast Visual Laser Not Working

I want to make a block along a raycast for the visual effect of a laser weapon. I’ve done this before with a really similar script, but this time, the origin of the laser is right but goes towards the workspace origin, or somewhere around there.
I haven’t found any help related to this, and the next part that I want to do requires this to be fixed.

local Direction = Origin.CFrame.LookVector * 5000

			local Hit = workspace:Raycast(Origin.Position,Direction,raycastParams)
			local Laser = game.ServerStorage.Laser:Clone()
			
			if Hit then
				Laser.Mesh.Scale = Vector3.new(0.5,0.5,Hit.Distance)
				Laser.CFrame = CFrame.new(Origin.Position,Direction) * CFrame.new(0,0,-Hit.Distance/2)

				local Explosion = Instance.new("Explosion")
				Explosion.Position = Hit.Position
				Explosion.BlastRadius = 10
				Explosion.Parent = workspace

				local Sound = game.ServerStorage.Explosion:Clone()
				Sound.Parent = Hit.Instance
				Sound:Play()
				game:GetService("Debris"):AddItem(Sound,Sound.TimeLength)
			else
				Laser.Mesh.Scale = Vector3.new(0.5,0.5,5000)
				Laser.CFrame = CFrame.new(Origin.Position,Direction) * CFrame.new(0,0,-5000/2)
			end

The screenshot shows the laser firing twice, both towards a completely different direction than where the turret was facing.

Replace this:

Laser.CFrame = CFrame.new(Origin.Position,Direction) * CFrame.new(0,0,-Hit.Distance/2)

With this:

local MidPoint = (Origin.Position + Hit.Position) / 2
Laser.CFrame = CFrame.lookAt(MidPoint, Origin.Position)

It worked, thanks!
I changed it up a tiny bit so it also works for when the raycast doesn’t intersect anything.

1 Like

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