Hello developers. I am currently making a laser gun with a long laser beam that shoots out when you click. However, the laser beam doesn't go straight, but it goes to the center of the object I am shooting at. I was wondering how I could make the laser beam go straight no matter what is in the way and so it doesn't just go to the center of the object.

Here is the local script located in the laser gun:

function CreateBullet(Origin, Direction)
	local MidPoint = Origin.Position + Direction/2
	
	local beam = Instance.new("Part", nil)
	beam.Name = "LaserBullet"
	beam.Anchored = true
	beam.CanCollide = false
	beam.Massless = true
	beam.Locked = true
	beam.Parent = workspace
	beam.BrickColor = Player.TeamColor
	beam.Material = Enum.Material.Neon
	beam.Transparency = 0
	beam.Size = Vector3.new(0.05, 0.05, Direction.Magnitude)
	beam.Orientation = Origin.Orientation
	beam.CFrame = CFrame.new(MidPoint, Origin.Position)
	
	-- + Vector3.new(3,-.5,0)
	
	Debris:AddItem(beam, 1)
	
	beam.Touched:Connect(function(Result)
		if Result and Result.Parent:FindFirstChild("Humanoid") and hitDebounce == false then
			hitDebounce = true
			local humanoid = Result.Parent:FindFirstChild("Humanoid")

			if humanoid and humanoid ~= Player.Character.Humanoid then
				if Result.Name == "Head" then
					humanoid:TakeDamage(ConfigScript.CriticalDamage)
				else
					humanoid:TakeDamage(ConfigScript.NormalDamage)
				end
			end
			wait(1)
			hitDebounce = false
		end
	end)
	
	for i= 0,1,0.1 do
		beam.Transparency = beam.Transparency + .1
		wait(.1)
	end
end

function Shoot(target)
	if not ShootCooldown and script.Parent.CurrentAmmo.Value > 0 and ReloadDebounce == false then
		ShootCooldown = true
		script.Parent.CurrentAmmo.Value -= 1
			if GunScreen.Visible == true then
				GunScreen.Screen.Ammo.Text = "Ammo: "..tostring(gun.CurrentAmmo.Value).."/"..tostring(gun.MaxAmmo.Value)
				if ReplicatedStorage.Settings.AutoReload.Value == true and script.Parent.CurrentAmmo.Value == 0 then
					Reload()
				end
			end
		if Player.Character:FindFirstChild(script.Parent.Name) then
			local sin = math.sin(tick()*RecoilSize)/RecoilSize
			CF = CF:Lerp(CFrame.new(ConfigScript.OffsetFromCamera.X, ConfigScript.OffsetFromCamera.Y,ConfigScript.OffsetFromCamera.Z+sin),.1)
			ReplicatedStorage.LaserGunShootingSound:Play()
			local Origin = gun.Origin
			local Direction = (Mouse.Hit.Position - Origin.Position).Unit * ((Mouse.Hit.Position - Origin.Position).Magnitude)
			CreateBullet(Origin, Direction)
			wait(ConfigScript.Cooldown_Between_Each_Click)
			ShootCooldown = false
		end
	elseif script.Parent.CurrentAmmo.Value <= 0 then
		GunScreen.Screen.Loaded.Text = "Must Reload!"
		ReplicatedStorage.ErrorSound:Play()
		ReplicatedStorage.ErrorSound.Stopped:Wait(.8)
	end
end

Thanks for reading!

1 comment

0

If the script is too big I could take out the stuff that have nothing to do with the bullet and repost it.