Hey there Dev Forum!
So I wanted to add idle camera bobbing to my script. The problem is, just nothing works.
I’ve tried everything that I know about and it still isn’t working.
Script (StarterCharacterScripts)
local runService = game:GetService("RunService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
function updateBobbleEffect()
if humanoid.FloorMaterial ~= Enum.Material.Air then
local currentTime = tick()
if humanoid.MoveDirection.Magnitude > 0 then -- we are not standing still
if humanoid.WalkSpeed < 18 then -- we are sprinting
local bobbleX = math.cos(currentTime * 5) * .1
local bobbleY = math.abs(math.sin(currentTime * 5)) * .2
local bobble = Vector3.new(bobbleX, bobbleY, 0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .25)
elseif humanoid.WalkSpeed > 18 then -- we are walking
humanoid.CameraOffset = humanoid.CameraOffset * 1
local bobbleX = math.cos(currentTime * 8) * .2
local bobbleY = math.abs(math.sin(currentTime * 8)) * .4
local bobble = Vector3.new(bobbleX, bobbleY, 0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .25)
else
local bobbleX = math.cos(currentTime * 2) * .2
local normalPos = Vector3.new(bobbleX, 0, 0)
local idleBobble = humanoid.CameraOffset:lerp(normalPos, 0.1)
idleBobble:Play()
end
end
end
end
runService.RenderStepped:Connect(updateBobbleEffect)
Hey, this post is an hour ago, but I fixed the script.
local runService = game:GetService("RunService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
function updateBobbleEffect()
if humanoid.FloorMaterial ~= Enum.Material.Air then
local currentTime = tick()
if humanoid.MoveDirection.Magnitude > 0 then -- we are not standing still
if humanoid.WalkSpeed < 18 then -- we are sprinting
local bobbleX = math.cos(currentTime * 5) * .1
local bobbleY = math.abs(math.sin(currentTime * 5)) * .2
local bobble = Vector3.new(bobbleX, bobbleY, 0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .25)
elseif humanoid.WalkSpeed > 18 then -- we are walking
humanoid.CameraOffset = humanoid.CameraOffset * 1
local bobbleX = math.cos(currentTime * 8) * .2
local bobbleY = math.abs(math.sin(currentTime * 8)) * .4
local bobble = Vector3.new(bobbleX, bobbleY, 0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .25)
end
else
local speed = currentTime * 1.75 -- higher = faster bob
local distance = .2 -- change for more drastic movement higher = more
local bobbleY = math.abs(math.sin(speed)) * distance
local bobble = Vector3.new(0, bobbleY, 0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .2)
end
end
end
runService.RenderStepped:Connect(updateBobbleEffect)
The problem was that you put the else statement inside the part where it checks if your walking or sprinting.