Help with gun bobbing

RunService.RenderStepped:Connect(function()
	ViewModel.Torso.CFrame = Camera.CFrame  -- Set viewmodel cframe to camera cframe
	if Character:FindFirstChildWhichIsA("Tool") and Humanoid.MoveDirection.Magnitude > 0 then
        -- player is moving with a tool equipped
		local currentCF = Camera.CFrame

		local x = math.cos(tick() * 3) * 0.2
		local y = math.abs(math.sin(tick() * 5)) * 0.1

		local cf = currentCF * CFrame.new(x, y, 0)

		ViewModel.Torso.CFrame = cf
	end
end)

The gun bobbing here works perfectly fine in my fps viewmodel, however once the player stops moving, it immediately goes back to the camera cframe which looks very jarring.
https://gyazo.com/625e2f87bf81b3e9eb02e8ce6ceb3e49

I had originally tried using the given example in this article to shift the viewmodel back smoothly.
(This part specifically)

humanoid.CameraOffset = humanoid.CameraOffset * 0.75

But I can’t apply this example to CFrames. Are there any other possible solutions?

Maybe try to lerp the CFrame to the Camera CFrame

ViewModel.Torso.CFrame = ViewModel.Torso.CFrame:Lerp(Camera.CFrame,0.07)

I’m not sure what’s the problem here, but if you want to, you should just use the spring module for bobbing. I can give you the bobbing code example if you’d like to.

Go ahead, although I try to avoid springs as I don’t understand them as well.

It’s not that hard, you just make a new instance with module.new() and do things.

Ok, first thing is get the module and put it in replicated storage and create a new instance.

local spring = require(game.ReplicatedStorage.SpringModule)
local bobbing = spring.new()

Then here’s a simple function I made for the bobbing.

local function Bobbing(addition)
	return math.sin(tick() * addition * 1.3) * 0.5 -- Increase the multiplication to make it faster!
end

Now you can simply do this.

game:GetService("RunService").RenderStepped:Connect(function(dt)
       local bob = Vector3.new(Bobbing(10),Bobbing(5),Bobbing(5))
       bobble:shove(bob / 10 * (char.HumanoidRootPart.Velocity.Magnitude / 10))
       local updatebob = bobble:update(dt)
       viewmodel.HumanoidRootPart.CFrame  = viewmodel.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(updatebob.Y, updatebob.X, 0))
end)

This is the code that how I use and make my viewmodel, you can edit it according to your dependencies.

Hope it helps. :smiley:

4 Likes

What exactly is bobble in the script?

Bumping the post. Don’t leave me hanging man

viewmodel.HumanoidRootPart.CFrame  = viewmodel.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(updatebob.Y, updatebob.X, 0))

This line bobbles.

Sorry for late reply.
You can replace HumanoidRootPart with Torso or whatever your viewmodel has.

image
I mean this line, you never define it in your previous post, what is bobble?

That’s the instance that we created with spring.new()

image

Wait what is the spring module you’re referring to? Is it this one?

SpringModule.rbxm (1.3 KB)
Take this module. Just drag and drop it in the explorer tab on your studio.

Should be the same.

2 Likes