Why wont my camera bob script work?

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

local player = Players.LocalPlayer
local character
local humanoid
local camera = workspace.CurrentCamera

local function onCharacterAdded(newCharacter)
    character = newCharacter
    humanoid = character:WaitForChild("Humanoid")
end

player.CharacterAdded:Connect(onCharacterAdded)

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

local function cameraBob()
    local amplitude = 0.05
    local frequency = 2
    local prevBobDirection = Vector3.new(0, 0, 0)

    while true do
        local dt = RunService.RenderStepped:Wait()
        if humanoid and humanoid.MoveDirection.Magnitude > 0 then
            local speed = humanoid.WalkSpeed / 16 -- Normalize walk speed
            local xOffset = amplitude * math.sin(workspace.DistributedGameTime * frequency * speed)
            local zOffset = amplitude * math.cos(workspace.DistributedGameTime * frequency * speed)
            local newBobDirection = Vector3.new(xOffset, 0, zOffset)

            -- Apply the camera bob
            camera.CFrame = camera.CFrame * CFrame.new(prevBobDirection):inverse() * CFrame.new(newBobDirection)
            prevBobDirection = newBobDirection
        else
            -- Reset the camera bob if the player is not moving
            camera.CFrame = camera.CFrame * CFrame.new(prevBobDirection):inverse()
            prevBobDirection = Vector3.new(0, 0, 0)
        end
    end
end

cameraBob()

My code refuses to work, no error either

1 Like

you shouldn’t use a while loop for camera bobbing instead try using the renderstepped event

replace:

while true do
        local dt = RunService.RenderStepped:Wait()
end

to:

RunService.RenderStepped:Connect(function(dt)
end)

still no bob… weird that it won’t work

I tested the script on studio and it does bob but only slightly you should probably add more amplitude or frequency to see it properly

script i used (slightly modified):

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

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera

local function cameraBob()
	local amplitude = 30.05
	local frequency = 2
	local prevBobDirection = Vector3.new(0, 0, 0)

	RunService.RenderStepped:Connect(function(dt)
		if humanoid and humanoid.MoveDirection.Magnitude > 0 then
			local speed = humanoid.WalkSpeed / 16 -- Normalize walk speed
			local xOffset = amplitude * math.sin(workspace.DistributedGameTime * frequency * speed)
			local zOffset = amplitude * math.cos(workspace.DistributedGameTime * frequency * speed)
			local newBobDirection = Vector3.new(xOffset, 0, zOffset)
			
			print(newBobDirection)

			-- Apply the camera bob
			camera.CFrame = camera.CFrame * CFrame.new(prevBobDirection):inverse() * CFrame.new(newBobDirection)
			prevBobDirection = newBobDirection
		else
			-- Reset the camera bob if the player is not moving
			camera.CFrame = camera.CFrame * CFrame.new(prevBobDirection):inverse()
			prevBobDirection = Vector3.new(0, 0, 0)
		end
	end)
end

cameraBob()
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.