essentially.... this is the code. I am trying to make a turret aim at a target using a rotation around the y axis (at base, moves whole thing) (working) and having the gun thingies themselves rotate around z-axis to aim at the target (only moves gun thingies).

the gun thingies work in terms of being rigged right and all, but idk how to get them to aim at the target right. here is the code: hinge_xyz is the gun thingies hinge of the turret, as i said hinge_xz is working perfectly well.

local firerate = 0.1
local targetfolder1 = game.ServerStorage.Enemies
local aggrodist = 100
local hinge_xyz = script.Parent.FakeHinge.HingeConstraint
local hinge_xz = script.Parent.TurretBase1.HingeConstraint
wait(10)
while wait(firerate) do
	local target = nil
	
	for i, v in pairs(game.Workspace:GetChildren()) do
		if targetfolder1:FindFirstChild(v.Name) then
			if (v["Pri. Part"].Position - script.Parent.Hitbox.Position).magnitude < aggrodist then
				target = v
				local shotray = Ray.new(script.Parent.Hitbox.Position, ((v["Pri. Part"].Position - script.Parent.Hitbox.Position).Unit * 500))
				--local hit, pos = game.Workspace:FindPartOnrayWithIgnoreList(shotray, {script.Parent})
				--if hit == target then
				local dummypart = Instance.new("Part")
				dummypart.Parent = workspace
				dummypart.Anchored = true
				local c_Frame = CFrame.new(script.Parent.Hitbox.Position, target:GetPrimaryPartCFrame().p)
				dummypart.CFrame = c_Frame
				local y = dummypart.Orientation.Y
				local x = dummypart.Orientation.X
				local z = dummypart.Orientation.Z
				hinge_xz.TargetAngle = y
				local newray = Ray.new(script.Parent.FakeHinge.Position, target["Pri. Part"].Position)
				local currentposray = Ray.new(script.Parent.FakeHinge.Position, script.Parent.Reference.Position)
				
				print(math.sin(math.rad(x)))
				if x < hinge_xyz.LowerAngle and x > hinge_xyz.UpperAngle then
					if x > hinge_xyz.LowerAngle then
						x = hinge_xyz.LowerAngle
					elseif x > hinge_xyz.UpperAngle then
						X = hinge_xyz.UpperAngle
					end
				end
				print(x, y, z)
				hinge_xyz.TargetAngle = x
				dummypart:Destroy()
				--end
			else
				hinge_xz.TargetAngle = 0
			end
		end
	end
end

please help.

1 comment

0

(Im no sure if this will help but) I've seen the dev king make a video about turrets, it can't shoot if there's an object in front of it (Just like real life) and stops shooting when the target is dead, anyways here's the link https://www.youtube.com/watch?v=CPcaJgQkxM8