Rotating Motor6Ds so characters look at enemies

Hey, I have this code here that’s supposed to rotate the upper parts of R6 towers towards the attacked enemy and it works fine for taller enemies, but when it comes to enemies with the same height as the tower - if they’re too close the tower will look up, which obviously doesn’t look good.


Not sure how to fix that behaviour, could anybody help?

local function rotateEntity(entity1Model: Model, entity2: Model)
	local entity2Attributes
	if entity2.Parent == workspace.Enemies or entity2.Parent == workspace.DeadEnemies then
		entity2Attributes = entity2:WaitForChild("Attributes", 1)
	else
		entity2Attributes = entity2:WaitForChild("TowerData", 1)
	end
	if (not entity2Attributes) or (not entity1Model.PrimaryPart) then return end

	local targetPosition = entity2Attributes:GetAttribute("Position")
	local targetCFrame = CFrame.lookAt(entity1Model.PrimaryPart.Position, Vector3.new(targetPosition.X, entity1Model.PrimaryPart.Position.Y, targetPosition.Z))

	entity1Model.PrimaryPart.CFrame = targetCFrame

	if entity1Model.Parent.Parent == workspace.Towers then
		local RHip = entity1Model.Torso:WaitForChild("Right Hip")
		local LHip = entity1Model.Torso:WaitForChild("Left Hip")
		local TorsoLookVector = entity1Model.Torso.CFrame.lookVector
		local HeadPosition = entity1Model.Head.CFrame.Position
		local Point = entity2Attributes:GetAttribute("Position")
	
		local Distance = (entity1Model.Head.CFrame.Position - Point).magnitude
		local Difference = entity1Model.Head.CFrame.Y - Point.Y
	
		local tower = ReplicatedStorage.TowerModels:FindFirstChild(entity1Model.Parent.Name) or ReplicatedStorage.TowerModels.Upgrades[entity1Model.Parent.Name]
		local LHipOriginC0 = tower.Torso["Left Hip"].C0
		local RHipOriginC0 = tower.Torso["Right Hip"].C0
	
		local NeckOriginC0 = tower.Torso["Neck"].C0
		local WaistOriginC0 = tower.PrimaryPart["RootJoint"].C0
			
		local goalNeckCFrame = CFrame.Angles((math.atan(Difference / Distance) * 0.7), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0)
		entity1Model.Torso.Neck.C0 = entity1Model.Torso.Neck.C0:lerp(goalNeckCFrame*NeckOriginC0, 0.7 / 2).Rotation + NeckOriginC0.Position
			
		local xAxisWaistRotation = (math.atan(Difference / Distance) * 0.7)
		local yAxisWaistRotation = (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.7
		local rotationWaistCFrame = CFrame.Angles(xAxisWaistRotation, yAxisWaistRotation, 0)
		local goalWaistCFrame = rotationWaistCFrame*WaistOriginC0
		entity1Model.PrimaryPart.RootJoint.C0 = entity1Model.PrimaryPart.RootJoint.C0:lerp(goalWaistCFrame, 0.7 / 2).Rotation + WaistOriginC0.Position
			
			
		local currentLegCounterCFrame = entity1Model.PrimaryPart.RootJoint.C0*WaistOriginC0:Inverse()
	
		local legsCounterCFrame = currentLegCounterCFrame:Inverse()
			
		RHip.C0 =  legsCounterCFrame*RHipOriginC0
		LHip.C0 = legsCounterCFrame*LHipOriginC0
	end
end