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