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