Tank Turret not Aiming Properly

Hello there! I am attempting to make a system in which the tank’s turret rotates to face a certain position. The turret’s rotation is controlled by two hinges, the horizontal hinge (turretHinges) and the vertical hinge (gunHinge). The vertical hinge is working as intended with one big problem: If the tank is sitting on a sloped surface the hinge’s target angle will be greatly offset from it’s intended destination.
I have tried everything in my power to fix this issue, but I have failed. Below you may find the code and a video showcasing the bug.

Code:

local hull = script.Parent.Hull
local turret = script.Parent.Turret

local gun = turret.Gun
local aimPart = turret.TurretH

local mainGyro = hull.BodyGyro
local turretHinge = aimPart.HingeConstraint
local gunHinge = turret.TurretV.HingeConstraint

local rotationIncrement = 2

while wait() do
	local target = workspace.TANKTARGETTEST
	
	local cf = CFrame.new(turret.TurretH.Position,target.Position)
	local x,y,z = cf:ToOrientation()
	
	turretHinge.TargetAngle = math.ceil(
		-(hull.Orientation.Y - math.deg(y)) / rotationIncrement
	)*rotationIncrement
	
	gunHinge.TargetAngle = math.ceil(
		-math.deg((x-z)) / rotationIncrement
	)*rotationIncrement
end

Here’s a video of the code in action

2 Likes

Hi, please refer to this post I made in a different thread, which has a solution extremely similar to what you are looking for:

Note that it works no matter how many servos you chain together and in what orientation because it’s doing all the math in worldspace.

1 Like

try using more servos

and if that doesnt work use more servos

and if perhaps that happens to not quite function that well you shall try using more servos

and if that solution doesnt really seem to quite satisfy your need of making a mini tank you shall and always shall try using more servos

1 Like

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