local function P(P)if not mobileShouldTrack then return end if player.Character.Humanoid.RigType==Enum.HumanoidRigType.R15 then return end if player.Character.Humanoid:GetState()~=Enum.HumanoidStateType.Swimming then local O=player.Character.HumanoidRootPart local Q=player.Character.Head local G=(P-Q.Position).unit local v=math.acos(G:Dot(Vector3.new(0,1,0)))local T=v if math.deg(T)>110 then T=math.rad(110)end neck.C0=CFrame.new(0,1,0)*CFrame.Angles(math.pi-T,math.pi,0)local n do n=player.Character:FindFirstChild("\082\105\103\104\116 \065\114\109")or player.Character:FindFirstChild("\082\105\103\104\116\085\112\112\101\114\065\114\109")end local j=O.Position+O.CFrame:vectorToWorldSpace(Vector3.new(O.Size.X/2+n.Size.X/2,O.Size.Y/2-n.Size.Z/2,0))local U=((P-j)*Vector3.new(1,0,1)).unit local a=(O.CFrame.lookVector*Vector3.new(1,0,1)).unit local x=math.acos(U:Dot(a))if tostring(x)=="-\049\046\035\073\078\068"then x=0 end if player.Character.Humanoid:GetState()==Enum.HumanoidStateType.Seated then local P=O.CFrame.lookVector:Cross(U)if x>math.pi/2 then x=math.pi/2 end if P.Y<0 then x=-x end end shoulder.C0=CFrame.new(1,.5,0)*CFrame.Angles(math.pi/2-v,math.pi/2+x,0)if not amISitting(player.Character)then O.CFrame=CFrame.new(O.Position,O.Position+(Vector3.new(P.X,O.Position.Y,P.Z)-O.Position).unit)end end end
-- Function to call on renderstepped. Orients the character so it is facing towards
-- the player mouse's position in world space. If character is sitting then the torso
-- should not track
local function frame(mousePosition)
	-- Special mobile consideration. We don't want to track if the user was touching a ui
	-- element such as the movement controls. Just return out of function if so to make sure
	-- character doesn't track
	if not mobileShouldTrack then
		return
	end

	--This math is completely wrong with R15. We're better off just not doing it at all
	if player.Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
		return
	end

	-- Make sure character isn't swiming. If the character is swimming the following code will
	-- not work well; the character will not swim correctly. Besides, who shoots underwater?
	if player.Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Swimming then
		local torso = player.Character.HumanoidRootPart
		local head = player.Character.Head

		local toMouse = (mousePosition - head.Position).unit
		local angle = math.acos(toMouse:Dot(Vector3.new(0, 1, 0)))

		local neckAngle = angle

		-- Limit how much the head can tilt down. Too far and the head looks unnatural
		if math.deg(neckAngle) > 110 then
			neckAngle = math.rad(110)
		end
		neck.C0 = CFrame.new(0, 1, 0) * CFrame.Angles(math.pi - neckAngle, math.pi, 0)

		-- Calculate horizontal rotation
		local arm
		do
			arm = player.Character:FindFirstChild("Right Arm") or player.Character:FindFirstChild("RightUpperArm")
		end
		local fromArmPos = torso.Position
			+ torso.CFrame:vectorToWorldSpace(
				Vector3.new(torso.Size.X / 2 + arm.Size.X / 2, torso.Size.Y / 2 - arm.Size.Z / 2, 0)
			)
		local toMouseArm = ((mousePosition - fromArmPos) * Vector3.new(1, 0, 1)).unit
		local look = (torso.CFrame.lookVector * Vector3.new(1, 0, 1)).unit
		local lateralAngle = math.acos(toMouseArm:Dot(look))

		-- Check for rogue math
		if tostring(lateralAngle) == "-1.#IND" then
			lateralAngle = 0
		end

		-- Handle case where character is sitting down
		if player.Character.Humanoid:GetState() == Enum.HumanoidStateType.Seated then
			local cross = torso.CFrame.lookVector:Cross(toMouseArm)
			if lateralAngle > math.pi / 2 then
				lateralAngle = math.pi / 2
			end
			if cross.Y < 0 then
				lateralAngle = -lateralAngle
			end
		end

		-- Turn shoulder to point to mouse
		shoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(math.pi / 2 - angle, math.pi / 2 + lateralAngle, 0)

		-- If not sitting then aim torso laterally towards mouse
		if not amISitting(player.Character) then
			torso.CFrame = CFrame.new(
				torso.Position,
				torso.Position + (Vector3.new(mousePosition.X, torso.Position.Y, mousePosition.Z) - torso.Position).unit
			)
		end
	end
end
