im doing a backrooms game and i want it realistic as possibble but i cant connect the bobbing and the tween service for crouching how can i do it
crouchTweenInfo = TweenInfo.new(0.35, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
Player.CameraMode = Enum.CameraMode.LockFirstPerson
function CrSway()
local goal = {}
if Humanoid then
if CrouchEnabled == false then
goal.CameraOffset = Vector3.new(0,-0.5,0)
elseif CrouchEnabled == true then
goal.CameraOffset = Vector3.new(0,-3,0)
end
end
local crouchTween = TweenService:Create(Humanoid, crouchTweenInfo, goal)
crouchTween:Play()
end
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local crouchTweenInfo = TweenInfo.new(0.35, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local CrouchEnabled = false
local crouchOffsetY = -0.5
local tiltAmount = 5
local currentTilt = 0
local turnSmoothing = 8
local bobSpeed = 10
local bobAmount = 0.15
UserInputService.InputBegan:Connect(function(input, gpe)
if gpe then return end
if input.KeyCode == Enum.KeyCode.LeftControl then
CrouchEnabled = not CrouchEnabled
CrSway()
end
end)
function CrSway()
local targetOffset = CrouchEnabled and -3 or -0.5
local goal = { CameraOffset = Vector3.new(0, targetOffset, 0) }
local tween = TweenService:Create(humanoid, crouchTweenInfo, goal)
tween:Play()
crouchOffsetY = targetOffset
end
RunService.RenderStepped:Connect(function(dt)
if not character or not rootPart then return end
local moveDirection = humanoid.MoveDirection.Magnitude
local bobOffset = 0
if moveDirection > 0 then
local time = tick() * bobSpeed
bobOffset = math.sin(time) * bobAmount
end
local velocity = rootPart.Velocity
local angularVelocity = rootPart.RotVelocity
local targetTilt = math.clamp(angularVelocity.Y * tiltAmount, -tiltAmount, tiltAmount)
currentTilt = currentTilt + (targetTilt - currentTilt) * math.clamp(turnSmoothing * dt, 0, 1)
local finalOffset = Vector3.new(0, crouchOffsetY + bobOffset, 0)
humanoid.CameraOffset = finalOffset
camera.CFrame = CFrame.new(camera.CFrame.Position)
* CFrame.Angles(0, 0, math.rad(currentTilt))
* CFrame.new(camera.CFrame.LookVector)
end)