-- returns the angles needed to have the npc look at a target


local function lookAt(target)
	local eye = (hrp.CFrame * CFrame.new(0, 3, 0)):pointToObjectSpace(target).unit;
	local horizontal = -math.atan2(eye.x, -eye.z);
	local vertical = math.asin(eye.y);
	
	local goal = Vector3.new();
	if not (math.abs(horizontal) > settings.horizontalRange or math.abs(vertical) > settings.verticalRange) then
		local hsign, habs = math.sign(horizontal), math.abs(horizontal);
		local hneck, hwaist = habs*0.5, habs*0.5;
		
		if (hwaist > settings.maxHorizontalWaist) then
			local remainder = hwaist - settings.maxHorizontalWaist;
			hwaist = settings.maxHorizontalWaist;
			hneck = math.clamp(hneck + remainder, 0, settings.maxHorizontalHead);
		end
		
		goal = Vector3.new(hsign*hneck, hsign*hwaist, vertical);
	end
	
	return goal;
end
