Struggling to get a vertical hinge to point at a part

I have 2 hinges setup - one to move horizontally and one to move vertically.

I want the hinges to find a part and point to it.

My horizontal hinge is working fine, however the vertical one seems to struggle to actually point at the right place. It almost seems like the part has to be the perfect distance for it to correctly point at it.

Here is my code:

local PartToTrack = script.ToTrack
repeat task.wait() until PartToTrack.Value ~= nil

local Horizontal = script.Parent.Rotate.Horizontal.RotateAround.HingeConstraint
local Vertical = script.Parent.Rotate.Horizontal.Vertical.RotateAround.HingeConstraint
local VerticalPos = script.Parent.Rotate.Horizontal.Vertical:FindFirstChild("Head") or script.Parent.Rotate.Horizontal.Vertical:FindFirstChild("RotateAround")

function ComputeTargetAngleY(TargetPos, StartPos)
	local X = TargetPos.X - StartPos.X
	local Y = TargetPos.Y - StartPos.Y
	local Angle = math.atan2(X, Y)
	Angle = math.deg(Angle)
	return Angle + 90
end

function ComputeTargetAngleX(TargetPos, StartPos)
	local X = TargetPos.Y - StartPos.Y
	local Z = TargetPos.Z - StartPos.Z
	local Angle = math.atan2(X, Z)
	Angle = math.deg(Angle)
	return Angle + 90
end

game:GetService("RunService").Heartbeat:Connect(function()
	Horizontal.TargetAngle = ComputeTargetAngleX(PartToTrack.Value.Position, Horizontal.Parent.Position)

	Vertical.TargetAngle = ComputeTargetAngleY(PartToTrack.Value.Position, VerticalPos.Position)
end)

Cheers :slightly_smiling_face:

(Edit: and no, removing the +90 does not help)

1 Like

Oh my god thank you so much! Works perfectly and I adjusted it to work for both vertical and horizontal :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.