I have a project I've been working on and I tried writing my own script for a turret that tracks the player when it gets near, although at the moment I have it look for me regardless of distance. So far I have the actual gun part rotating properly, but not the thing it's attached to. It's literally the same code except using the Y axis instead of the X axis since it's mounted sideways on a wall.

Here's my code:

local turret = script.Parent
local gunHinge = turret.GunBase.Base.Base2GunHinge
local gunHingePos = gunHinge.Attachment0.WorldPosition
local baseHinge = turret.WallMount.Mount.Base2WallHinge
local baseHingePos = baseHinge.Attachment0.WorldPosition

local target

local function trackObject(object)
	local gunHingeX = object.Position.X - gunHingePos.X
	local gunHingeZ = object.Position.Z - gunHingePos.Z
	local baseHingeY = object.Position.Y - baseHingePos.Y
	local baseHingeZ = object.Position.Z - baseHingePos.Z
	local ratio1 = gunHingeX/gunHingeZ
	local ratio2 = baseHingeY/baseHingeZ
	
	gunHinge.TargetAngle = -math.deg(math.atan(ratio1))
	baseHinge.TargetAngle = -math.deg(math.atan(ratio2))
end

while wait() do
	trackObject(game.Workspace:WaitForChild("acer1102").HumanoidRootPart)
end

Thanks in advance! :D

2 comments

0

What do you mean by lower hinge and what exactly is the problem

0

I have turret I built. It uses Hinge Constraints. The top hinge works perfectly. The bottom hinge does not. Here's a GIF of said problematic turret: https://media.giphy.com/media/ZE57kO7sx1iHd7wegy/giphy.gif