-- perform the update loop
-- do the R6 update loop


stepped_con = game:GetService("RunService").RenderStepped:Connect(function()
	debug.profilebegin("FPS")
	swaysize = script.WalkSwayMultiplier.Value


	-- checkfirstperson() checks if camera is first person and enables/disables the viewmodel accordingly
	checkfirstperson()
	-- update loop
	if isfirstperson == true then
		local modifierr = 0
		local statething = player:GetAttribute('State')
		-- make arms visible
		if larm.LocalTransparencyModifier ~= 0 then
			visiblearms(true)
		end
		-- update walk sway if we are walking
		if isrunning == true and includewalksway and humanoid:GetState() ~= Enum.HumanoidStateType.Freefall and humanoid:GetState() ~= Enum.HumanoidStateType.Landed then
			walksway = walksway:lerp(
				CFrame.new(

					(0.07*swaysize) * math.sin(tick() * (2 * humanoid.WalkSpeed/4)),
					(0.07*swaysize) * math.cos(tick() * (4 * humanoid.WalkSpeed/4)),
					0

				)*
					CFrame.Angles(

						0,
						0,
						(-.03*swaysize) * math.sin(tick() * (2 * humanoid.WalkSpeed/4))

					)
				,0.2*sensitivity)
		else
			walksway = walksway:Lerp(CFrame.new(), 0.05*sensitivity)
		end
		--
		local delta = uis:GetMouseDelta()
		--
		if includecamerasway then
			sway = sway:Lerp(Vector3.new(delta.X,delta.Y,delta.X/2), 0.1*sensitivity)
		end
		--
		if includestrafe then
			strafesway = strafesway:Lerp(CFrame.Angles(0,0,-rootpart.CFrame.rightVector:Dot(humanoid.MoveDirection)/(20/swaysize)), 0.1*sensitivity)
		end
		--
		if includejumpsway == true then
			jumpsway = jumpswaygoal.Value
		end
		-- update animation transform for viewmodel
		rightshoulderclone.Transform = rightshoulder.Transform
		leftshoulderclone.Transform = leftshoulder.Transform
		-- cframe the viewmodel

		if statething ~= nil and statething == "Crouching" and player.Character:FindFirstChildWhichIsA("Tool") then
			modifierr = 20
		end

		if statething ~= nil and statething == "Crawling" and player.Character:FindFirstChildWhichIsA("Tool") then
			modifierr = 80
		end

		local CamY, CamX, CamZ = camera.CFrame:ToEulerAnglesYXZ()
		
		local direction = Vector2.new(
			math.rad(player.Character.HumanoidRootPart.CFrame.RightVector:Dot((-player.Character.HumanoidRootPart.Velocity * Vector3.new(1,0,1)).Unit) * 200),
			math.rad(player.Character.HumanoidRootPart.CFrame.LookVector:Dot((-player.Character.HumanoidRootPart.Velocity * Vector3.new(1,0,1)).Unit) * 200)
		)
		
		local tilt = direction.Unit * math.clamp((player.Character.HumanoidRootPart.Velocity.Magnitude / 14), 0, 2)
		
		local firstframe = CFrame.new( Vector3.new(camera.CFrame.Position.X,camera.CFrame.Position.Y,camera.CFrame.Position.Z) ) * CFrame.fromEulerAnglesXYZ(CamY + math.rad(modifierr),0,0)
		local TorsX,TorsY,TorsZ = player.Character.Torso.CFrame.Rotation:ToEulerAnglesXYZ()
		local torsoframe = CFrame.Angles(TorsX,TorsY,TorsZ)
		
		local countercf = CFrame.new()
		

		if script.CFrameAimOffset.Value.Position.X ~= 0 or not (script.CFrameAimOffset.Value.Position.X <= 0.1) then
			--countercf = CFrame.Angles(math.rad(-tilt.Y * 3),math.rad(-tilt.X * 4),math.rad(-tilt.X * 6))
		end 
		
		local finalcf = (CFrame.new(firstframe.Position)*torsoframe*firstframe.Rotation*walksway*jumpsway*strafesway*countercf*CFrame.Angles(math.rad(sway.Y*swaysize),math.rad(sway.X*swaysize)/10,math.rad(sway.Z*swaysize)/2))+(camera.CFrame.UpVector*(-1.7-(headoffset.Y+(aimoffset.Value.Y))))+(camera.CFrame.LookVector*(headoffset.Z+(aimoffset.Value.Z)))+(camera.CFrame.RightVector*(-headoffset.X-(aimoffset.Value.X)+(-(sway.X*swaysize)/75)))
	
		
		local builtcf = finalcf * script.CFrameAimOffset.Value:Inverse()
		
		viewmodel:SetPrimaryPartCFrame(builtcf)
	end
	debug.profileend()
end)