Using parts of EgoMoose’s “First Person Element of an FPS”, and Headstackk’s “How to animate Tool Parts (Guns, Knifes etc.)”, I used a ViewModel for a First Person view for various weapons, using an AnimationController inside of the Model to play animations.
The ViewModel uses RunService.RenderStepped to update the ViewModel’s CFrame to the Camera’s CFrame. My current problem is how to make the gun bob when you move (not swaying, bobbing).
How should I go about doing this? I’ve searched numerous times for bobbing but all that comes up is about gun swaying.
I have also attempted to use math.sin() and math.cos() to create a bobbing effect, but I don’t really have a clear understanding of how to implement those functions into the ViewModel’s movement.
So I recently found out how to do this. Basically, just take the current tick() value and use said value to input into the math.cos() and math.sin() functions. Turn the values into a Vector3, then a CFrame. Multiply the Camera’s Current CFrame with the other CFrame to get an offsetted CFrame.
local function update()
local currentCF = workspace.CurrentCamera.CFrame
local t = tick()
local x = math.cos(t * 5) * 0.25
local y = math.abs(math.sin(t * 5)) * 0.25
local cf = currentCF * CFrame.new(x, y, 0)
head.CFrame = cf
end
game:GetService("RunService").RenderStepped:connect(update)