Laser Homing not working properly

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

here’s the video link: lasers.mp4 - Google Drive

here’s the code:

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
2 Likes

This is the part I would do differently:

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

Hope this helped!

2 Likes

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)

3 Likes

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

1 Like

The Extensions is added ontop of the distance between player and spawnPos

2 Likes

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

1 Like

Like I explained above, it does not have to because it is being added ontop of that distance

Are my changes working?

1 Like

they aren’t working but you fixed the wild behavior of once going fast slow, up and down

1 Like

I don’t see the issue can you maybe send me the full code your are using rn?

also the lasers aren’t pointing at the character

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
2 Likes

Please use this version:

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]!)

its working with aiming but you forgot about the orientation so now all of the lasers are in the same orientation

1 Like

I see another problem with my idotic code!
change this:

local direction = (pos1 - pos2).Unit

to that:

local direction = (pos2 - pos1).Unit

And what is not working about the orientation?

1 Like

Change the size of the deadray object so that the front face is actually the front face

1 Like

the orientation isn’t changing still
are sure player position (pos2) should be the orientation?

1 Like

No. Use this as the size for your object: (2, 2, 5)

already changed, so the orientation should be the direction?

1 Like

the pos2 is the position that the dead ray will look at its not orientation or anything
(read your dm’s)

it doesn’t look like its changing
image