Hey so im making an obby and there is a part where you have to dodge lasers but the homing is wrong ( it started when i changed the n in the pos3 formula from 2 to 300 / (pos1 - pos2).Magnitude ) and the lasers aren’t pointing at you and the speed is inconsistent
function laser()
if workspace[workspace.char.Value.Name].ClassName == "Model" then
if workspace.char.Value:FindFirstChild("HumanoidRootPart") then
local c = game.ReplicatedStorage.dead:Clone()
local pos1 = script.Parent.Position
local pos2 = workspace.char.Value.HumanoidRootPart.Position
local distance = 300 / (pos1 - pos2).Magnitude
-- pos3 uses a formula for extending a line based on 2 points (x2 * n - x1, y2 * n - y1, z2 * n - z1)
-- where n is the multiplier of how much to extend
local pos3 = Vector3.new(pos2.X * distance - pos1.X, pos2.Y * distance - pos1.Y,pos2.Z * distance - pos1.Z)
c.Parent = workspace
c.Position = script.Parent.Position
-- orientation uses direction formula (pos1 - pos2) but scaled to magnitude 1 and multiplied by 180
c.Orientation = (pos2 - pos1).Unit * 180
local Service = game:GetService("TweenService")
local tween = Service:Create(c,TweenInfo.new(pos3.Magnitude /75,Enum.EasingStyle.Linear),{Position = pos3})
tween:Play()
end
end
end
while true do
laser()
wait(4)
end
local extension = 30 --studs
local pos1 = script.Parent.Position --Vec3
local pos2 = workspace.char.Value.HumanoidRootPart.Position --Vec3
local direction = (pos1 - pos2).Unit --Vec3 with length of 1
local pos3 = pos2 + (direction * extension) --Vec3
And then I wouldn’t use pos3.Magnitude in (length / velocity) I would use:
local distance = (pos2 - pos1).Magnitude --studs
local speed = 75 --studs/s
local Service = game:GetService("TweenService")
local tween = Service:Create(c,TweenInfo.new(distance/speed, Enum.EasingStyle.Linear), {Position = pos3})
tween:Play()
The distance is only between origin and player because it makes it clearer how the speed seems
I cleaned up your code and added my changes hope this works and let me know!
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
--
local function laser()
if workspace[workspace.char.Value.Name].ClassName == "Model" then
if workspace.char.Value:FindFirstChild("HumanoidRootPart") then
local extension = 30
local speed = 75
local c = ReplicatedStorage:WaitForChild("dead"):Clone()
local pos1 = script.Parent.Position
local pos2 = workspace.char.Value.HumanoidRootPart.Position
local direction = (pos1 - pos2).Unit
local pos3 = pos2 + (direction * extension)
local fullDistance = (pos1 - pos3).Magnitude
c.Parent = workspace
c.Position = script.Parent.Position
-- orientation uses direction formula (pos1 - pos2) but scaled to magnitude 1 and multiplied by 180
c.Orientation = (pos2 - pos1).Unit * 180
local t = fullDistance / speed
local tweenInfo = TweenInfo.new(t,Enum.EasingStyle.Linear)
local tween = TweenService:Create(c,tweenInfo,{CFrame = CFrame.new(pos3)})
tween:Play()
end
end
end
while true do
laser()
task.wait(4)
end
(btw. always use local functions if you don’t use them globally)
why did you make the extension a fixed value? the player moves and the x rotates so the distance changes and a fixed value wont do it ( you can watch the video to know what i mean )
the extension must get bigger when the distance between player and laser shooter increases and it has to get smaller if it decreases
thats what we do right here - we add the extension to the pos2 (we basically converted those 30 studs to a Vec3 and add that to the pos2 to get pos3) : SO everything should be dynamic and not fixed
function laser()
if workspace[workspace.char.Value.Name].ClassName == "Model" then
if workspace.char.Value:FindFirstChild("HumanoidRootPart") then
local c = game.ReplicatedStorage.dead:Clone()
local pos1 = script.Parent.Position --Vec3
local pos2 = workspace.char.Value.HumanoidRootPart.Position --Vec3
local direction = (pos1 - pos2).Unit --Vec3 with length of 1
local extension = 1000 --studs
local pos3 = pos2 + (direction * extension) --Vec3
c.Parent = workspace
c.Position = script.Parent.Position
-- orientation uses direction formula (pos1 - pos2) but scaled to magnitude 1 and multiplied by 180
c.Orientation = (pos2 - pos1).Unit * 180
local distance = (pos2 - pos1).Magnitude --studs
local speed = 200 --studs/s
local Service = game:GetService("TweenService")
local tween = Service:Create(c,TweenInfo.new(distance/speed, Enum.EasingStyle.Linear), {Position = pos3})
tween:Play()
end
end
end
while true do
laser()
wait(4)
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
--
local function laser()
if workspace[workspace.char.Value.Name].ClassName == "Model" then
if workspace.char.Value:FindFirstChild("HumanoidRootPart") then
local extension = 30
local speed = 100
local c = ReplicatedStorage:WaitForChild("dead"):Clone()
local pos1 = script.Parent.Position
local pos2 = workspace.char.Value.HumanoidRootPart.Position
local direction = (pos1 - pos2).Unit
local pos3 = pos2 + (direction * extension)
local distance1 = (pos1 - pos2).Magnitude
c.Parent = workspace
c.CFrame = CFrame.new(script.Parent.Position,pos2)
local t = distance1 / speed
local tweenInfo = TweenInfo.new(t,Enum.EasingStyle.Linear)
local tween = TweenService:Create(c,tweenInfo,{CFrame = CFrame.new(pos3)})
tween:Play()
end
end
end
while true do
laser()
task.wait(4)
end
Try it again and let me know! (DONT CHANGE EXTENSION [for now]!)