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?