# Aiming laser not aiming correctly

so i want to make sniper helicopters where it shoots from inside as if there were snipers inside but the aiming is totally incorrect

i tried CFrame’s orientation using origin and look at vectors but it wasn’t accurate
then i tried trigonometry but its very inaccurate but while there was only 1 snipe helicopter then it worked then when it worked i copied it to the other side and put a minus before the first atan2 function it worked but its not working on the other “left and right” where the first helicopter is the back (i also experimented with the X Y Z values in the formula but it lead to nothing so i left it as it was before experimenting)

it has to do something with the trigonometry function being positive or negative and the input to them

Code:

``````local function laser()
local p = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers() )]
local char = p.Character

local pos1 		= script.Parent.Position

-- for now randomly offsets the position for the shots to be **Slightly** innacurate

local pos2_5    =  pos2 + Vector3.new(math.random(-15,15),math.random(-15,15),math.random(-15,15))

local extension = (pos1 - pos2_5).Magnitude /2 + 15
local direction = (pos2_5 - pos1).Unit
local pos3 = pos1 + (direction * extension)
local c 		= game.ReplicatedStorage:WaitForChild("Part"):Clone()
local fullDistance = (pos1 - pos2_5).Magnitude + 30
c.Parent = workspace

-- this bit uses a formula that i forgot the details of because i copied the code from my other project (like an average person does)

local posx =  math.sqrt(math.abs(pos2_5.X - pos1.X)^2 + math.abs(pos2_5.Z - pos1.Z)^2)
local posy =  math.sqrt(math.abs(pos2_5.Z - pos1.Z)^2 + math.abs(pos2_5.Y - pos1.Y)^2)
local posz =  math.sqrt(math.abs(pos2_5.Y - pos1.Y)^2 + math.abs(pos2_5.X - pos1.X)^2)
local pos22 = pos2_5 - pos1

-- place with trigonometric functions

c.Orientation = Vector3.new(math.atan2(pos22.Z/posy,pos22.X/posy) * 57.2957795,math.atan2(pos22.X/posy,pos22.Z/posy) * 57.2957795,0)

c.Size = Vector3.new(0.3,fullDistance,0.3)
c.Position = pos3
c.Color = Color3.new(1, 1, 0)
c.Script.Enabled = true
c:Destroy()
end

while true do
laser()
end
``````

the video:

1 Like

For anyone wondering:
I fixed it now (friend of @grzegorz11115)!

``````	local distance = (target - origin).Magnitude + laserExtension
local direction = (target - origin).Unit
local centerPosition = origin + (direction*distance/2)

part.CFrame = CFrame.LookAt(centerPosition,target)
``````

Basically we get the position between the target and origin by adding the distance/2 with respect to the direction to the origin and then create a cframe at that center and make it look at the target (which gets our rotation right without weird trig functions)

1 Like

Also: For anyone wanting the lasers to predict player movements you can use the following function:

``````local function predict(part, timeInterval)
return part.Position + part.Velocity * timeInterval
end
``````

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