Загрузка данных


-- Camera Effects
-- LocalScript -> StarterPlayer -> StarterPlayerScripts

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer

local NORMAL_FOV = 70
local SPRINT_FOV = 76

local WALK_BOB_SPEED = 8
local RUN_BOB_SPEED = 12

local WALK_BOB_AMOUNT = 0.035
local RUN_BOB_AMOUNT = 0.06

local SIDE_AMOUNT = 0.012
local TILT_AMOUNT = 0.008

local SMOOTHNESS = 8
local FOV_SMOOTHNESS = 6

local character
local humanoid
local rootPart

local bobTime = 0
local currentOffset = Vector3.zero
local currentTilt = 0

local function setupCharacter(char)
	character = char
	humanoid = char:WaitForChild("Humanoid")
	rootPart = char:WaitForChild("HumanoidRootPart")
end

if player.Character then
	setupCharacter(player.Character)
end

player.CharacterAdded:Connect(setupCharacter)

RunService:BindToRenderStep(
	"CameraEffects",
	Enum.RenderPriority.Camera.Value + 1,
	function(deltaTime)

		local camera = workspace.CurrentCamera

		if not character or not humanoid or not rootPart then
			return
		end

		if humanoid.Health <= 0 then
			return
		end

		local velocity = Vector3.new(
			rootPart.AssemblyLinearVelocity.X,
			0,
			rootPart.AssemblyLinearVelocity.Z
		)

		local speed = velocity.Magnitude
		local moving = speed > 1

		local state = humanoid:GetState()

		local airborne =
			state == Enum.HumanoidStateType.Jumping
			or state == Enum.HumanoidStateType.Freefall
			or state == Enum.HumanoidStateType.FallingDown

		local sprinting = speed >= 22

		if moving and not airborne then

			local bobSpeed = sprinting
				and RUN_BOB_SPEED
				or WALK_BOB_SPEED

			local bobAmount = sprinting
				and RUN_BOB_AMOUNT
				or WALK_BOB_AMOUNT

			bobTime += deltaTime * bobSpeed

			local vertical = math.sin(bobTime * 2) * bobAmount
			local horizontal = math.cos(bobTime) * SIDE_AMOUNT
			local tilt = math.sin(bobTime) * TILT_AMOUNT

			local targetOffset = Vector3.new(
				horizontal,
				vertical,
				0
			)

			currentOffset = currentOffset:Lerp(
				targetOffset,
				math.clamp(deltaTime * SMOOTHNESS, 0, 1)
			)

			currentTilt +=
				(tilt - currentTilt)
				* math.clamp(deltaTime * SMOOTHNESS, 0, 1)

		else

			currentOffset = currentOffset:Lerp(
				Vector3.zero,
				math.clamp(deltaTime * SMOOTHNESS, 0, 1)
			)

			currentTilt +=
				(0 - currentTilt)
				* math.clamp(deltaTime * SMOOTHNESS, 0, 1)

		end

		local targetFOV = sprinting and moving
			and SPRINT_FOV
			or NORMAL_FOV

		camera.FieldOfView +=
			(targetFOV - camera.FieldOfView)
			* math.clamp(deltaTime * FOV_SMOOTHNESS, 0, 1)

		camera.CFrame =
			camera.CFrame
			* CFrame.new(currentOffset)
			* CFrame.Angles(0, 0, currentTilt)
	end
)