Missile Turret Won't Follow Target Accurately?

I am making a missile turret that will be used to track planes that close to it via following said planes, and then eventually launching a missile at them.

However, I am having problems getting the turret to accurately track/follow targets. The yaw works half-well, and then stops really working when the part is in almost a “blind spot” of the turret (which I don’t want, I want the turret to be able to move 360 degrees fully), and it won’t even attempt to adjust the pitch based on the height/elevation of the target… You can see the above scenarios in the below video:

I am using the following script:

local hinge = script.Parent.movers.spin
local hinge2 = script.Parent.movers.elevate
local hingeBase = hinge and hinge.Attachment0
local target = workspace.target1

local function calculateServoAngle(hinge,target)
	local Turn,Tilt,Roll = hinge.Attachment1.WorldCFrame:ToObjectSpace(CFrame.new(hinge.Attachment0.WorldPosition, target.Position)):ToOrientation();
	return math.deg(Turn)-90;
end;

local function calculateServoAngle2(hinge,target)
	local Turn,Tilt,Roll = hinge.Attachment1.WorldCFrame:ToObjectSpace(CFrame.new(hinge.Attachment0.WorldPosition, target.Position)):ToOrientation();
	return math.deg(Tilt)-90;
end;

game["Run Service"].Heartbeat:Connect(function()
	if hingeBase then
		--local origin = hingeBase.CFrame
		--local displacement = target.Position - hingeBase.Position

		local turn = calculateServoAngle(hinge,target)
		hinge.TargetAngle = -turn
		
		local tilt = calculateServoAngle2(hinge2,target)
		hinge2.TargetAngle = tilt
	end
end)

Here is the model itself:
samturret.rbxm (16.8 KB)

Not sure what I am doing wrong here, am I screwing up the setup for the hinges?

1 Like

Do not use math.degree

use math.rad instead

This is not that helpful, no offense.

I believe that the CFrames Rotations are actually still aaplied when using attachment.WorldCFrame, Instead why not use
Cframe.new(attachment.WorldPosition) then do the operation

This resulted in the turning of the turret getting even more broken.

my bad I figured out that by turning the CFrames Lookvector upwards and then applying :ToEulersXYZ Should give us the Roll of the hinge by using Yaw instead (In which the only important part about a hinge is the Roll), so I came up with this

local function calculateServoAngle(hinge,target)
	local Turn,Tilt,Roll = (hinge.Attachment1.WorldCFrame:ToObjectSpace(CFrame.new(hinge.Attachment0.WorldPosition, target.Position))*CFrame.Angles(math.rad(90),0,0)):ToEulerAnglesXYZ();
	return math.deg(Turn)+90;
end;

local function calculateServoAngle2(hinge,target)
	local Turn,Tilt,Roll = (hinge.Attachment1.WorldCFrame:ToObjectSpace(CFrame.new(hinge.Attachment0.WorldPosition, target.Position))*CFrame.Angles(math.rad(90),0,0)):ToEulerAnglesXYZ();
	return math.deg(Turn)-90;
end;

I was originally going to use ToEulerAngles:(enum.RotationOrder.ZYX) but I forgot that when using lookat that the Roll is basically never applied