Hi, I’ve been testing around my tank turret and noticed when the turret is tilted on an axis the gun position will not be accurate to the mouse position. I’m debating using cFrame to move the gun to the mouse but I’d like to learn more with hinges and angles.
My issue is when the Turret is rotated on either X or Z axis the aim of the gun is displaced
I tried to make it so when the turret is tilted in either the X or Z axis it would add or subtract that axis to TargetAngle of the hinge for the gun but it didn’t work.
By the way, I’m setting my “turretOrientation” to this part here:
Heres 2 videos of my issue:
local replicatedStorage = game:GetService("ReplicatedStorage")
remoteEvent = replicatedStorage:WaitForChild("Mousepos")
gunPosEvent = replicatedStorage:WaitForChild("GunPosEvent")
local Seat = game.Workspace.KV2Turret.VehicleSeat
Seat.Changed:Connect(function()
if Seat.Occupant ~= nil then
-- define some variables
Occupant = Seat.Occupant
Hinge = game.Workspace.KV2Turret.Part.TurretRotator
RootPart = Hinge.Parent
Player = game.Players.LocalPlayer
mouse = Player:GetMouse()
mouse2 = Player:GetMouse()
while wait() do
-- define position of origin
local HKpos = game.Workspace.KV2Turret.Part.Position
-- define position of target
local position = mouse.hit.p
-- the ratio to get the triangle's width over the height
local ratio = (position.X-HKpos.X)/(position.Z-HKpos.Z)
local beta = math.atan(ratio) -- converting this to radians
-- turn it from radians into degrees
local GunPos = game.Workspace.KV2Turret.Gun.Gunrotate.Position
local mouseVertical = mouse.hit.p
local ratio2 = (mouseVertical.Y-GunPos.Y)/(mouseVertical.Z-GunPos.Z)
local beta2 = math.atan(ratio2)
beta2 = beta2*(180/math.pi)
beta = beta*(180/math.pi)
if (position.Z-HKpos.Z)<0 then
if (position.X-HKpos.X)<0 then -- Quad 3
beta = 180 + beta
else -- Quad 4
beta = -180 + beta
end
end
local turretOrientation = game.Workspace.KV2Turret.Turret.Body.Orientation
if (position.Z-turretOrientation.Z)<0 then
if (position.X-turretOrientation.X)<0 then -- Quad 3
beta2 = 180 + beta
else -- Quad 4
beta2 = -180 + beta
end
end
print(beta2)
remoteEvent:FireServer(beta)
gunPosEvent:FireServer(beta2)
end
end
end)
The remote events just take the beta and beta2 angles and set them as the TargetAngle for the hinges