I have a WW2-era AA gun that I had a develop model for, but I rigged it up + scripted it. The turret moves around via the player’s mouse.
It moves up and down, turns side-to-side, etc., but it is EXTREMELY unstable, especially for up and down movements. Here’s of video of that.
Here’s the (local) script for it:
local player = game.Players.LocalPlayer
local val = script:WaitForChild("connection",3)
local connection = val.Value
local Weapon = connection
local TurnTurret = Weapon:FindFirstChild("turnGun");
local AngleGun = Weapon:FindFirstChild("gunUpDown");
local Mouse = player:GetMouse()
local function calculateServoAngle(hinge, targetPosition)
local baseAttachment = hinge.Attachment0
local object_horizontal_offset = (baseAttachment.WorldCFrame):PointToObjectSpace(targetPosition)
local object_yaw_angle = math.atan2(object_horizontal_offset.Y, object_horizontal_offset.Z)
object_yaw_angle = math.deg(object_yaw_angle)
return object_yaw_angle
end
game["Run Service"].RenderStepped:Connect(function(step)
Mouse.TargetFilter = workspace;
local Hit = Mouse.Hit;
local HitPosition = Hit.Position;
--print(tostring(HitPosition))
TurnTurret.TargetAngle = calculateServoAngle(TurnTurret,HitPosition)
AngleGun.TargetAngle = calculateServoAngle(AngleGun,HitPosition)
end)
Here is the HingeConstrain:
How can I fix this?