Help with FPS viewmodel bobbing

Hey There!, What i need is a good method to make bobbing for my viewmodel, what I have now is a decent controller,
https://gyazo.com/6bfaba907250a7b948de2d63e6fce2d9

The Code (literally just this):

but what I want to achive is something like this:
https://gyazo.com/8c2e168cb53ef9cfbedf971fafa7b77d

2 Likes

Using a sine or cosine wave should be able to provide that bobbing motion. This is actually a very good method since you can make the bobbing more aggressive by changing the graph’s amplitude.

local function update()
    local t = tick()

    if humanoid.MoveDirection.Magnitude > 0 then
        -- do math
        local x = math.cos(t * 5) * 0.25
        local y = math.abs(math.sin(t * 5)) * 0.25

        local cf = CFrame.new(x, y, 0)
        ViewModel.Head.CFrame = Camera.CFrame * cf
    else
        ViewModel.Head.CFrame = Camera.CFrame
    end
end

This code I found on a different post and was just to give you an idea of the math equations and how they are used.

6 Likes

That looks perfect, I’ll quickly give it a try. thank you so much.

Alright, thanks to you i got a good idea of the math. https://gyazo.com/45636983c026da8f7692eddf580fb4b1
now though. i need to find a way to make the transition from bobbing and idle actually smooth.
any suggestions?

I guess you could use some tweening, try using TweenService:GetValue()

Try this

local TweenService = game:GetService("TweenService")
local TweenTime = 0.3 -- Amount of time for transmission
local startTime = 0
local t = tick()

local function update()
    if humanoid.MoveDirection.Magnitude > 0 then
        t = tick()
        startTime = 0
        -- do math
        local x = math.cos(t * 5) * 0.25
        local y = math.abs(math.sin(t * 5)) * 0.25

        local cf = CFrame.new(x, y, 0)
        ViewModel.Head.CFrame = Camera.CFrame * cf
    else
        if startTime == 0 then startTime = tick() end
        -- do math
        local x = math.cos(t * 5) * 0.25
        local y = math.abs(math.sin(t * 5)) * 0.25

        local alpha = TweenService:GetValue(math.min((tick() - startTime) / TweenTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
        local cf = CFrame.new(x * alpha, y * alpha, 0)
        ViewModel.Head.CFrame = Camera.CFrame
    end
end

What is an alpha value? 30 characters

Updating every time

RunService.RenderStepped

Doesn’t make the tween even noticable, so what should I do? fire every .3 seconds?

Thank you so much for the help btw

Could I see a GIF? (30 characters)
or you could try increasing tween time
Oh wait I think I Found out what the problem is, change the alpha variable line to
local alpha = 1 - TweenService:GetValue(math.min((tick() - startTime) / TweenTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
I forgot to invert the value so it goes from 1 to 0 instead of 0 to 1

Sure you can, here: https://gyazo.com/041f744d96f287f944999f90dd828d51

Apparentlly it still looks like the gif i just sent. Btw i can send the place if you want to.

I’m not sure what the problem is but I’ve done some changes here and hopefully it works this time

local TweenService = game:GetService("TweenService")
local TweenTime = 0.3 -- Amount of time for transmission
local startTime = 0
local t = tick()

local function update()
    if humanoid.MoveDirection.Magnitude > 0 then
        t = tick()
        startTime = 0
        -- do math
        local x = math.cos(t * 5) * 0.25
        local y = math.abs(math.sin(t * 5)) * 0.25

        local cf = CFrame.new(x, y, 0)
        ViewModel.Head.CFrame = Camera.CFrame * cf
    else
        if startTime < 1 then startTime = tick() end
        -- do math
        local x = math.cos(t * 5) * 0.25
        local y = math.abs(math.sin(t * 5)) * 0.25

        local alpha = 1- (TweenService:GetValue(math.min((tick() - startTime) / TweenTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out))
        local cf = CFrame.new(x * alpha, y * alpha, 0)
        ViewModel.Head.CFrame = Camera.CFrame * cf
    end
end
1 Like

I finally have everything i need!. thank you so much. if you want the place feel free to dm me, Have an amazing day kind sir.

(yes it worked)

You are welcome :slight_smile:
Can I see how it looks like now?

https://gyazo.com/a9c156877422a3f298eb8a68acaddb4d like this

im having some problems udjusting the intensity of the bobbing, so uh, if you dont mind could i know how?, apparentlly once you change it from the x and y value it’s still not enough. thanks again

just multiply the x and y values
so it’s something like

local intensity = 1.2

x * intensity

1 Like

I think this should have all your needs

local TweenService = game:GetService("TweenService")
local TweenTime = 0.3 -- Amount of time for transmission
local startTime = 0
local Intensity = 1.2
local lastState = 0
local t = tick()

local function update()
    if humanoid.MoveDirection.Magnitude > 0 then
        t = tick()
        if lastState == 0 then lastState = 1 startTime = tick() end
        -- do math
        local x = math.cos(t * 5) * 0.25
        local y = math.abs(math.sin(t * 5)) * 0.25

        local alpha = (TweenService:GetValue(math.min((tick() - startTime) / TweenTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out))
        local cf = CFrame.new(x * alpha * Intensity, y * alpha * Intensity, 0)
        ViewModel.Head.CFrame = Camera.CFrame * cf
    else
        if lastState == 1 then lastState = 0 startTime = tick() end
        -- do math
        local x = math.cos(t * 5) * 0.25
        local y = math.abs(math.sin(t * 5)) * 0.25

        local alpha = 1- (TweenService:GetValue(math.min((tick() - startTime) / TweenTime, 1), Enum.EasingStyle.Cubic, Enum.EasingDirection.Out))
        local cf = CFrame.new(x * alpha * Intensity, y * alpha * Intensity, 0)
        ViewModel.Head.CFrame = Camera.CFrame * cf
    end
end
5 Likes

Oh thank you so much!, didnt even have to change some settings, you are amazing.

also changed the solution to that

2 Likes

https://gyazo.com/c722e760d67e556b2ffc6f6a0c563536 look at this. it’s amazing.

2 Likes

Please explain alpha values, if you don’t mind.

Oh sorry I forgot to reply, It’s a a float between 0 and 1 that I used for tweening, so If I multiply alpha by a value It’s the same as tweening it but this one works with my own calculation, so If i want to start a tween from the middle then I can just give it alpha = 0.5 and then start going up to 1
I used cubic easing so here is how it should look like
image

1 Like