I am working on a tank system with a turret and gun that follows the player’s mouse position. The turret appears to be working fine but the gun does not seem to align with the mouse.
Here is a video:
I tried adjusting the hinge constraints, orientation of the barrel and it’s constraints, and adjusting the code. But nothing seems to be working.
Here is the server script:
local function calculateServoAngle(turretHinge, mouse)
local baseAttachment = turretBase:WaitForChild("Attachment1")
local verticalBaseAttatchment = turret:WaitForChild("Attachment0")
local object_horizontal_offset = (baseAttachment.WorldCFrame):PointToObjectSpace(mouse)
local object_vertical_offset = (verticalBaseAttatchment.WorldCFrame):PointToObjectSpace(mouse)
local object_yaw_angle = math.atan2(-object_horizontal_offset.Y, -object_horizontal_offset.Z)
local object_yaw_angle2 = math.atan2(object_vertical_offset.Z, -object_vertical_offset.X)
object_yaw_angle = math.deg(object_yaw_angle)
object_yaw_angle2 = math.deg(object_yaw_angle2)
turret.TurretConstraint.TargetAngle = object_yaw_angle
turret.BarrelConstraint.TargetAngle = object_yaw_angle2
end
And here is the local script:
remoteEvent.OnClientEvent:Connect(function()
inTurret = true
camera.CameraType = Enum.CameraType.Follow
camera.CameraSubject = turret
end)
runService.Heartbeat:Connect(function()
if inTurret == true then
local mouse = plr:GetMouse()
local mousePosition = mouse.Hit.Position
remoteEvent:FireServer(mousePosition)
end
end)
I would check the rotation of your attachments (make sure they go forwards) and check that the axes are correct in the lines above.
The problem also might be that the barrel’s attachment’s CFrame (assuming it’s rotated by the turret) needs to be rotated to the desired turret rotation before calculating the angle it needs to be at.
I can’t open the Windows video file, so I can’t be sure about the set up.
So it looks like the horizontal motion is aiming properly, but the vertical barrel is being aimed incorrectly. Specifically around 0:02-0:04 in the video it seems to oscillate as the turret is rotating horizontally.
That means this line isn’t working properly, potentially from this line:
I don’t really want to try to modify your math , so here is some new stuff that should work:
local turretPosition = baseAttachment.WorldCFrame.Position
local barrelPosition = verticalBaseAttatchment.WorldCFrame.Position
local targetPosition = mouse
local height = targetPosition.Y - hingePosition.Y
local turretRadius = Vector2.new(turretPosition.X - barrelPosition.X, turretPosition.Y - barrelPosition.Y).Magnitude
local targetTurretXZDistance = Vector2.new(targetPosition.X - turretPosition.X, targetPosition.Z - turretPosition.Z).Magnitude
-- We want the angle the barrel should be at if it were already aligned
local length = targetTurretXZDistance - turretRadius
local barrelAngle = math.arctan2(height, length)
(Note that this assumes the turret rotation is perpendicular the XZ plane. If this is a problem convert the vectors to a CFrame where the turret rotation axis is perpendicular to the XZ plane of the coordinate frame.)
I believe one problem with your code is that the barrel doesn’t take into account it’s 3D offset from the turret’s rotation, so when the turret isn’t aligned it doesn’t aim properly.