Scripting Helpers is winding down operations and is now read-only. More info→
Log in to vote

the current speed to affect headbob after?

Asked by 6 months ago

my script is

local runService = game:GetService("RunService") -- Runs something every single frame
local currentSpeed = 0
local character = script.Parent -- this script will be in the actual player because its in StarterPlayerScripts
local pos = character.HumanoidRootPart.Position
local humanoid = character:WaitForChild("Humanoid") -- wait for child because we dont want to index it if its not loaded yet as that would cause errors

function updateHeadBobble()
    local currentTime = tick() -- tick(): gets current "epoc" time stamp
    if humanoid.MoveDirection.Magnitude > 0 then -- Mangitude: The distance from 0 so if the magnitude is bigger than 0, the player is moving
        pos = character.HumanoidRootPart.Position
        currentSpeed = (pos-character.HumanoidRootPart.Position).magnitude
        local bobbleX = math.cos(currentTime * 7.5) * currentSpeed -- bobbles in a cosine graph motion,an infinite ~
        local bobbleY = math.abs(math.sin(currentTime * 7.5)) * currentSpeed -- bobbles in a sine graph motion, practically the same as cosine but a bit offset
        -- math.abs: gets rid of all negative values and turns them positive
        local bobble =, bobbleY, 0)
        humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, 0.25) -- lerp: linear interpolation, second number in lerp is the percentage you want to interpolate in each frame, we're interpolating because if we didnt it would be a sharp cut to the bobbing when you do start moving in the game
    else -- player is not moving
        humanoid.CameraOffset = humanoid.CameraOffset * 0.75 -- bring camera back to not bobbing in a smooth way

--runService: the service
--RenderStepped: every frame on the server
--Connect(updateHeadBobble): Connects the function to the service so it runs function for every frame on server

It works but it happens after i move but i want the headbob to happen whilst im moving Anyone got any idea???

Answer this question