Hi, I’m making a tank turret using a hinge and would want it to slowly rotate to the position of the mouse and I got it working almost.
My issue is, however, it only rotates around 180 degrees in front of the turret then looks the opposite way of the mouse when the mouse is behind the turret
Oh and also it seems like the turret is frozen for the first 5-10 seconds when I jump into the seat and i have no clue what’s causing it
I tried looking for solutions on google but I can’t find any that use a hinge like I do
Heres a video of my issue:
Here’s my script its a local script located in the player scripts but im gonna try and change it later to server sided
local Seat = game.Workspace["KV-2 Turret"].VehicleSeat
Seat.Changed:Connect(function()
if Seat.Occupant ~= nil then
-- define some variables
local Occupant = Seat.Occupant
local Hinge = game.Workspace["KV-2 Turret"].Part.TurretRotator
local RootPart = Hinge.Parent
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
while wait() do
print(Occupant)
-- define position of origin
local HKpos = game.Workspace["KV-2 Turret"].Part.Position
print(HKpos)
-- define position of target
local position = mouse.hit.p
print(position)
-- 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
print(beta)
-- turn it from radians into degrees
beta = beta*(180/math.pi)
print(beta)
Hinge.TargetAngle = beta
end
end
end)
Well from your output the targetAngle range is -90 to 90; should be -180 to 180. This behavior derives from tangent which only ranges -90 to 90 degrees repeatedly.
So differentiate between quadrants 3 and 4 from 1 and 2. Like when the mouse is in quadrants 1 and 3, they are both positive ratios; 2 and 4 are both negative; and the absolute value ranges from 0 to 90 in each quadrant.
Here’s the behavior I think you’re looking for, when calculating beta; where x is the calculated degrees
Quadrant 1 and 2: x
Quadrant 3: 180 + x
Quadrant 4: -180 + x
Adjusted End Code
-- turn it from radians into degrees
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
Hinge.TargetAngle = beta
my math is a bit off but I remember learning quadrants. Thanks for explaining it in detail, however, do you know any reason why the turret freezes for the first few seconds then moves?
Sorry I don’t, the function isn’t yielding during the freeze because you’re getting output. So I think it might have to do with the hinge properties or physics engine.
Experiment with maxtorque, angularspeed; remove friction with customPhysicalProperties in the parts used. Make sure limitsEnabled is false.
I would also set a connection with the changed event(), and printing out any properties that get changed (excluding targetAngle) on the hinge and possibly on one turret part connected to the hinge. If instantly after some certain property is printed, the freeze disappears then that property might be connected to the problem.
So try those debug ideas out, if you’re still having trouble after that then let me know