How would I make my laser stop at hit

How would I make my laser stop at hit instead of the direction?

local deb = game:GetService("Debris")
local shot = game:GetService("ReplicatedStorage").shot
local rs = game:GetService("ReplicatedStorage")
local range = 200
local headDam = 100
local dam = 10

local function laser(dir, originPos, hit)
	local mid = originPos + dir / 2
	
	local laser = Instance.new("Part")
	laser.Name = "laser"
	laser.Parent = workspace.Lasers
	laser.BrickColor = BrickColor.new("Really red")
	laser.Material = Enum.Material.Neon
	laser.CanCollide = false
	laser.Anchored = true
	laser.CFrame = CFrame.new(mid, originPos)
	laser.Size = Vector3.new(.5,.5, dir.magnitude)
	
	deb:AddItem(laser, 0.5)
end


shot.OnServerEvent:Connect(function(player, hit, originPos)
	local hitPos = hit.Position
	local dir = (hitPos - originPos).Unit * range
	local res = workspace:Raycast(originPos, dir)
	if res then
		local char = res.Instance.Parent
		local hum = char:FindFirstChild("Humanoid")
		if hum and hum ~= player.Character.Humanoid then
			if res.Instance.Name == "Head" then
				hum:TakeDamage(headDam)
			else
				hum:TakeDamage(dam)
			end
		end
	laser(dir, originPos, hit)
	end
end)


1 Like

You can calculate the distance between the origin and hit point, and set that to the laser’s size.

Code:

local deb = game:GetService("Debris")
local shot = game:GetService("ReplicatedStorage").shot
local rs = game:GetService("ReplicatedStorage")
local range = 200
local headDam = 100
local dam = 10

local function laser(dir, originPos, hit)
	local mid = originPos + dir / 2

	local laser = Instance.new("Part")
	laser.Name = "laser"
	laser.BrickColor = BrickColor.new("Really red")
	laser.Material = Enum.Material.Neon
	laser.CanCollide = false
	laser.Anchored = true
	laser.CFrame = CFrame.new(mid, originPos)
	laser.Size = Vector3.new(.5,.5, (hit - originPos).Magnitude)
	laser.Parent = workspace.Lasers

	deb:AddItem(laser, 0.5)
end

shot.OnServerEvent:Connect(function(player, hit, originPos)
	local hitPos = hit.Position
	local dir = (hitPos - originPos).Unit * range
	local res = workspace:Raycast(originPos, dir)
	if res then
		local char = res.Instance.Parent
		local hum = char:FindFirstChild("Humanoid")
		if hum and hum ~= player.Character.Humanoid then
			if res.Instance.Name == "Head" then
				hum:TakeDamage(headDam)
			else
				hum:TakeDamage(dam)
			end
		end
		laser(dir, originPos, hit)
	end
end)
3 Likes

Magnitude is not a valid member of CFrame - Server - GunServer:18

That’s because Magnitude is a property of Vector3. What we’re looking for here is (hit - originPos).Position.Magnitude.

Still doesn’t work just with no errors.

change this to
local mid = originPos + (dir / 2)
because roblox calculates stuff from left to right and doesn’t follow pemdas except for parenthesis

also, can we see the local script for this?

Yeah no it still doesn’t work…

so the you said the problem is your laser doesn’t stop at where it hits and keeps going until it is 200 studs long, this might be the problem

if dir is being multiplied by the range then its going to go past the point because you use dir for mid and the size, so dir should be defined like this

local dir = hitPos - originPos

Nope still doesn’t work…
is it impossible or something?

what is the code in the local script, in case theres a problem there

If you would of realized that I posted this saying how would I make my laser stop at hit that means there was a laser before these edits meaning it was working before, in that case using all changes above makes the laser not appear, so means the problem is not the local script.

I really need help so I’m bumping

I’m not sure if this is the root of the problem, but it seems like there was a type mismatch when we do (hit - originPos).Magnitude. We should instead be doing (hit.Position - originPos).Magnitude, since hit was a CFrame.

It would be very useful to add type checking to this code. Would prevent something like this from happening.

Edit: I think I’ve figured it out. Here are the significant changes to be made:

-- distance between the origin point and the hit point
local distance = (hit.Position - originPos).Magnitude
...
laser.Size = Vector3.new(.5,.5, distance)
laser.CFrame = CFrame.new(originPos, hit.Position) 
                * CFrame.new(0, 0, -distance/2)

The reason we use CFrame.new(0, 0, -distance / 2) is that without it, the beam would be positioned the same as the originPos, whereas we want it to be in the middle of originPos and hit.Position. And so, since the beam is already facing the hit point, we move the beam forward by half of its own length, which automatically puts it in the desired location.

1 Like

I accidentally used the CFrame instead of a position.

Code:

local deb = game:GetService("Debris")
local shot = game:GetService("ReplicatedStorage").shot
local rs = game:GetService("ReplicatedStorage")
local range = 200
local headDam = 100
local dam = 10

local function laser(dir, originPos, hit)
	local mid = originPos + dir / 2

	local laser = Instance.new("Part")
	laser.Name = "laser"
	laser.BrickColor = BrickColor.new("Really red")
	laser.Material = Enum.Material.Neon
	laser.CanCollide = false
	laser.Anchored = true
	laser.CFrame = CFrame.new(mid, originPos)
	laser.Size = Vector3.new(.5,.5, (hit.Position - originPos).Magnitude)
	laser.Parent = workspace.Lasers

	deb:AddItem(laser, 0.5)
end

shot.OnServerEvent:Connect(function(player, hit, originPos)
	local hitPos = hit.Position
	local dir = (hitPos - originPos).Unit * range
	local res = workspace:Raycast(originPos, dir)
	if res then
		local char = res.Instance.Parent
		local hum = char:FindFirstChild("Humanoid")
		if hum and hum ~= player.Character.Humanoid then
			if res.Instance.Name == "Head" then
				hum:TakeDamage(headDam)
			else
				hum:TakeDamage(dam)
			end
		end
		laser(dir, originPos, hit)
	end
end)
1 Like

No need to be rude, he was correct. Check your laser code, you’re using hit which is a CFrame, when you need to use hit.Position. Your rudeness at people who are giving you help (where the help is actually correct) doesn’t make anyone more likely to help you in the future.

1 Like

Oh crap if you saw my last reply sorry just realized what you meant will try thanks.

EDIT: WORKS THANK YOU

Just realized. Thought he meant direction not distance😵

1 Like

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