function CharacterRealism.UpdateLookAngles(Q,s)local w,t=Q:ComputeLookAngle()Q:OnLookReceive(Q.Player,w,t)local A=Q.LastUpdate or 0 local x=os.clock()if x-A>.5 then w=Util:RoundNearestInterval(w,.05)t=Util:RoundNearestInterval(t,.05)if w~=Q.Pitch then Q.Pitch=w Q.Dirty=true end if t~=Q.Yaw then Q.Yaw=t Q.Dirty=true end if Q.Dirty then Q.Dirty=false Q.LastUpdate=x Q.SetLookAngles:FireServer(w,t)end end local M=workspace.CurrentCamera local O=M.CFrame.Position local X=Q.Player local n for s,w in pairs(Q.Rotators)do if not s.Parent then if not n then n={}end n[s]=true continue end local t=Players:GetPlayerFromCharacter(s)local A=t and t:DistanceFromCharacter(O)or 0 if t~=X and A>30 then continue end local M=w.LastStep or 0 local D=x-M local Z=s:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")local L=Z and Z.RootPart if not L then continue end local g=w.Pitch Q:StepValue(g,D)local a=w.Yaw Q:StepValue(a,D)local q=w.Motors w.LastStep=x if not q then continue end for Q,t in pairs(Q.RotationFactors)do local A=q and q[Q]if not A then continue end local x=A.Motor local M=A.Origin if M then local s=x.Part0 local Q=M.Parent if s and s~=Q then local Q=s:FindFirstChild(M.Name)if Q and Q:IsA("\065\116\116\097\099\104\109\101\110\116")then M=Q A.Origin=Q end end M=M.CFrame elseif A.C0 then M=A.C0 else continue end local O=g.Current or 0 local X=a.Current or 0 if w.SnapFirstPerson and Q=="\072\101\097\100"then end local n=O*t.Pitch local D=X*t.Yaw if Q:sub(-4)==" \065\114\109"or Q:sub(-8)=="\085\112\112\101\114\065\114\109"then local w=s:FindFirstChildOfClass("\084\111\111\108")if w and not CollectionService:HasTag(w,"\078\111\065\114\109\082\111\116\097\116\105\111\110")then if Q:sub(1,5)=="\082\105\103\104\116"and L:GetRootPart()~=L then n=O*1.3 D=X*1.3 else D=X*.8 end end end local Z=false if n~=g.Value then g.Value=n Z=true end if D~=a.Value then a.Value=D Z=true end if Z then local s=M-M.Position local Q=CFrame.Angles(0,n,0)*CFrame.Angles(D,0,0)x.C0=((M*s:Inverse())*Q)*s end end end if n then for s in pairs(n)do local w=Q.Rotators[s]local t=w and w.Listener if t then t:Disconnect()end Q.Rotators[s]=nil end end end


function CharacterRealism:UpdateLookAngles(delta)
	-- Update our own look-angles with no latency
	local pitch, yaw = self:ComputeLookAngle()
	self:OnLookReceive(self.Player, pitch, yaw)
	
	-- Submit our look-angles if they have changed enough.
	local lastUpdate = self.LastUpdate or 0
	local now = os.clock()
	
	if (now - lastUpdate) > .5 then
		pitch = Util:RoundNearestInterval(pitch, .05)
		yaw = Util:RoundNearestInterval(yaw, .05)

		if pitch ~= self.Pitch then
			self.Pitch = pitch
			self.Dirty = true
		end

		if yaw ~= self.Yaw then
			self.Yaw = yaw
			self.Dirty = true
		end
		
		if self.Dirty then
			self.Dirty = false
			self.LastUpdate = now
			self.SetLookAngles:FireServer(pitch, yaw)	
		end
	end
	
	-- Update all of the character look-angles
	local camera = workspace.CurrentCamera
	local camPos = camera.CFrame.Position
	
	local player = self.Player
	local dropList
	
	for character, rotator in pairs(self.Rotators) do
		if not character.Parent then
			if not dropList then
				dropList = {}
			end
			
			dropList[character] = true
			continue
		end
		
		local owner = Players:GetPlayerFromCharacter(character)
		local dist = owner and owner:DistanceFromCharacter(camPos) or 0

		if owner ~= player and dist > 30 then
			continue
		end
		
		local lastStep = rotator.LastStep or 0
		local stepDelta = now - lastStep

		local humanoid = character:FindFirstChildOfClass("Humanoid")
		local rootPart = humanoid and humanoid.RootPart

		if not rootPart then
			continue
		end
		
		local pitchState = rotator.Pitch
		self:StepValue(pitchState, stepDelta)

		local yawState = rotator.Yaw
		self:StepValue(yawState, stepDelta)
		
		local motors = rotator.Motors
		rotator.LastStep = now
		
		if not motors then
			continue
		end
		
		for name, factors in pairs(self.RotationFactors) do
			local data = motors and motors[name]

			if not data then
				continue
			end

			local motor = data.Motor
			local origin = data.Origin
			
			if origin then
				local part0 = motor.Part0
				local setPart0 = origin.Parent
				
				if part0 and part0 ~= setPart0 then
					local newOrigin = part0:FindFirstChild(origin.Name)

					if newOrigin and newOrigin:IsA("Attachment") then
						origin = newOrigin
						data.Origin = newOrigin
					end
				end
				
				origin = origin.CFrame
			elseif data.C0 then
				origin = data.C0
			else
				continue
			end

			local pitch = pitchState.Current or 0
			local yaw = yawState.Current or 0

			if rotator.SnapFirstPerson and name == "Head" then
				
			end

			local fPitch = pitch * factors.Pitch
			local fYaw = yaw * factors.Yaw

			-- HACK: Make the arms rotate with a tool.
			if name:sub(-4) == " Arm" or name:sub(-8) == "UpperArm" then
				local tool = character:FindFirstChildOfClass("Tool")
				
				if tool and not CollectionService:HasTag(tool, "NoArmRotation") then
					if name:sub(1, 5) == "Right" and rootPart:GetRootPart() ~= rootPart then
						fPitch = pitch * 1.3
						fYaw = yaw * 1.3
					else
						fYaw = yaw * .8
					end
				end
			end

			local dirty = false

			if fPitch ~= pitchState.Value then
				pitchState.Value = fPitch
				dirty = true
			end

			if fYaw ~= yawState.Value then
				yawState.Value = fYaw
				dirty = true
			end

			if dirty then
				local rot = origin - origin.Position
				
				local cf = CFrame.Angles(0, fPitch, 0)
				         * CFrame.Angles(fYaw, 0, 0)
				
				motor.C0 = origin * rot:Inverse() * cf * rot
			end
		end
	end
	
	-- If the dropList is declared, remove any characters that 
	-- were indexed into it. This is done after iterating over
	-- the rotators to avoid problems with removing data from
	-- a table while iterating over said table.
	
	if dropList then
		for character in pairs(dropList) do
			local rotator = self.Rotators[character]
			local listener = rotator and rotator.Listener
			
			if listener then
				listener:Disconnect()
			end
			
			self.Rotators[character] = nil
		end
	end
end
