Tank barrel problems

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)

Any suggestions would be greatly appreciated.

better if you can convert it to mp4

1 Like

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.

Sorry about that, I converted the video to mp4

Changed the file to mp4, sorry about that

1 Like

the only weird part i see is when it goes up near the start when looking at the other tank

are the limits on rotation not intentional . ?

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 :sweat_smile:, 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.

1 Like

Thank you very much for your advice and the code, the barrel is much smoother to control and the problem I was having seems to have been solved!

1 Like

Thank you for replying to my problem, the issue has been resolved.

1 Like

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