# How would I make viewmodel bobbing

Can someone help me understand how I would make ViewModel bobbing custom. I know that there is many sources where I can grab scripts from like FE gun kit or ACS but i want to really be able to understand how procedural ViewModel bobbing works so if anyone can help that would be great.

1 Like

you can simulate viewmodel bobbing either by animating it, or using humanoid.CameraOffset to change the offset of the player camera.

1 Like

I’m talking about procedural ViewModel bobbing not animated kinda like this:

``````local function ViewmodelBob(a, r, basewalkspeed)
local a, r = a or 1, r or 1
local d, s, v = distance * 6.28318 * 3 / 4, speed, -velocity
--if s < basewalkspeed then
local w = Vector3.new(r * math.sin(d / 4 - 1) / 256 + r * (math.sin(d / 64) - r * v.z / 4) / 512, r * math.cos(d / 128) / 128 - r * math.cos(d / 8) / 256, r * math.sin(d / 8) / 128 + r * v.x / 1024) * s / 20 * 6.28318
return CFrame.new(r * math.cos(d / 8 - 1) * s / 196, 1.25 * a * math.sin(d / 4) * s / 512, 0) * Math.FromAxisAngle(w)
--else
--local w = Vector3.new((r * math.sin(d / 4 - 1) / 256 + r * (math.sin(d / 64) - r * v.z / 4) / 512) * s / 20 * 6.28318, (r * math.cos(d / 128) / 128 - r * math.cos(d / 8) / 256) * s / 20 * 6.28318, r * math.sin(d / 8) / 128 * (5 * s - 56) / 20 * 6.28318 + r * v.x / 1024)
--return CFrame.new(r * math.cos(d / 8 - 1) * (5 * s - 56) / 196, 1.25 * a * math.sin(d / 4) * s / 512, 0) * Math.FromAxisAngle(w)
--end
end

``````
1 Like

Oooh, I get it. Do you mean this? This uses CFrames to move the viewmodel into a position still relative to the camera using trigonometric functions.

`a = (CFrame.new(math.cos(tick()*4) * 0.05, -humanoid.CameraOffset.Y/3, 0) * CFrame.Angles(0, math.sin(tick() * -4) * -0.05, math.cos(tick() * -4) * 0.05))`

1 Like

Thank you so much for the script. Can you go into detail on how I would be able to use CFrame to move a ViewModel into a position using trig functions? so like how i can understand it

1. First, create a new Animation object in the game and name it “ViewmodelBobbing”. This will be the animation that plays the bobbing motion.
2. Next, create a new Script object and place it in the game. This script will be responsible for playing the viewmodel bobbing animation based on the player’s movement.
3. Inside the script, use the `Humanoid.MoveDirection` property to detect when the player is moving. You can do this by creating a function that is called every frame, and checking the `MoveDirection` property inside the function.
4. When the player is moving, use the `Play` function on the animation instance to start the bobbing motion. You can use the `Humanoid.MoveDirection` property to adjust the speed and intensity of the bobbing motion.
5. Finally, you will need to attach the animation to the player’s weapon. You can do this by creating a Model object and placing the weapon inside it, and then setting the Model’s `PrimaryPart` property to the weapon object. The animation can then be attached to the Model object, and it will play on the weapon.

Here is an example of what the script might look like:

``````local viewmodelBobbing = script.Parent:WaitForChild("ViewmodelBobbing")

function onHumanoidMove(humanoid)
local moveDirection = humanoid.MoveDirection

-- If the player is moving, play the viewmodel bobbing animation
if moveDirection.Magnitude > 0 then
viewmodelBobbing:Play()
else
viewmodelBobbing:Stop()
end
end

-- Add the onHumanoidMove function as a callback for the Humanoid.MoveDirectionChanged event
script.Parent.Humanoid.MoveDirectionChanged:Connect(onHumanoidMove)
``````

This should give you a basic idea of how to set up custom viewmodel bobbing in Roblox. I hope this helps! Let me know if you have any questions or need further clarification.

3 Likes

My question is how would I be able to use math to make procedural ViewModel bobbing not an animation but thank you for helping

To create procedural viewmodel bobbing in Roblox without using an animation, you can use a combination of math and code to create the desired motion. Here is an example of how you might go about it:

1. First, create a new Script object and place it in the game. This script will be responsible for generating the viewmodel bobbing motion procedurally.
2. Inside the script, you will need to define some variables that control the motion of the viewmodel. These might include the amplitude (how far the viewmodel moves up and down), the frequency (how quickly the viewmodel moves), and the phase (the starting position of the viewmodel). You can adjust these variables to achieve the desired motion.
3. Next, you will need to create a function that generates the viewmodel bobbing motion based on these variables. One way to do this is to use a sine wave to generate the motion. You can do this by using the `math.sin` function in combination with the current time and the defined variables.
4. Finally, you will need to update the position of the viewmodel based on the generated motion. You can do this by using the `CFrame` and `Vector3` objects to set the viewmodel’s position and orientation.

Here is an example of what the script might look like:

``````local amplitude = 0.1 -- How far the viewmodel moves up and down
local frequency = 1 -- How quickly the viewmodel moves
local phase = 0 -- The starting position of the viewmodel

function updateViewmodelPosition()
local currentTime = tick() -- Get the current game time
local viewmodel = script.Parent -- Get the viewmodel object
local newPosition = Vector3.new(0, amplitude * math.sin(currentTime * frequency + phase), 0) -- Generate the new position using a sine wave
viewmodel.CFrame = viewmodel.CFrame:Add(newPosition) -- Update the
``````

This should give you an idea of how to create procedural viewmodel bobbing in Roblox using math and code. I hope this helps! Let me know if you have any questions or need further clarification.

3 Likes

can you explain how I would make the required trigonometric equations for bobbing

You will need to put a localscript into StarterPlayerScripts, define the viewmodel (make sure to parent that to the camera), and other things and then use CFrame.Angles to make the viewmodel sway.

``````local swayCF = CFrame.new()
RunService.RenderStepped:Connect(function() -- when doing this it is good to use a RenderStepped
if humanoid then -- just checking as precaution
local rot = camera.CFrame:ToObjectSpace(lastCameraCF) -- converts a CFrame relative to the object relative to a specific part
local X,Y,Z = rot:ToOrientation()
swayCF = swayCF:Lerp(CFrame.Angles(math.sin(X) * swayAMT, math.sin(Y) * currentSwayAMT, 0), 0.1) -- :Lerp to make it not snap back. Use CFrame.Angles to rotate an object on an axis
``````
1 Like

@esadams188’s responses aren’t theirs, they’re from ChatGPT.

Usually you modify Humanoid.CameraOffset. You can do physics based with a spring equation (look up spring module), or you can do something with a constant formula (like from a sine wave).

1 Like

To create viewmodel bobbing using trigonometry, you will need to define some variables that control the motion of the viewmodel. These might include the amplitude (how far the viewmodel moves up and down), the frequency (how quickly the viewmodel moves), and the phase (the starting position of the viewmodel). You can adjust these variables to achieve the desired motion.

Next, you will need to create a function that generates the viewmodel bobbing motion based on these variables. One way to do this is to use the `math.sin` function to generate a sine wave, which will be used to calculate the vertical position of the viewmodel at any given time. To create the sine wave, you can use the following equation: `y = A * sin(B * x + C)` , where `A` is the amplitude, `B` is the frequency, `x` is the current time, and `C` is the phase.

You can then use this equation to calculate the new position of the viewmodel at each frame, and update the viewmodel’s position using the `CFrame` and `Vector3` objects. This will create a smooth, realistic viewmodel bobbing motion.

I hope this helps! Let me know if you have any questions or need further clarification.

1 Like

can you help me understand how to make these formulas for bobbing? Also another question would I be able to convert animation RootJoint movement into a trignomic function for ViewModel bobbing

To create the formula for the viewmodel bobbing motion, you can use the `math.sin` function in combination with these variables. The `math.sin` function generates a sine wave, which can be used to calculate the vertical position of the viewmodel at any given time. The sine wave is defined by the following equation: `y = A * sin(B * x + C)` , where `A` is the amplitude, `B` is the frequency, `x` is the current time, and `C` is the phase.

You can then use this equation to calculate the new position of the viewmodel at each frame, and update the viewmodel’s position using the `CFrame` and `Vector3` objects. This will create a smooth, realistic viewmodel bobbing motion.

As for your second question, it is possible to convert an animation’s root joint movement into a trigonometric function for viewmodel bobbing in Roblox. However, this process can be quite complex and may require a good understanding of math and programming. Here is a high-level overview of how you might go about it:

1. First, create an animation that includes the desired root joint movement. You could do this by exporting a video of someone walking or running with a weapon, and then using an animation software to extract the desired motion and import it into Roblox.
2. Next, use a program or script to analyze the animation and extract the root joint movement data. This might involve calculating the position and orientation of the root joint at each frame of the animation.
3. Once you have extracted the root joint movement data, you can try to fit a trigonometric function (such as a sine wave) to the data. This might involve using a curve fitting algorithm or manually adjusting the variables of the trigonometric function to match the data as closely as possible.
4. Finally, you can use the fitted trigonometric function to generate the viewmodel bobbing motion in real-time, using the same method described earlier (i.e. using the `math.sin` function in combination with the variables of the function to calculate the new position of the viewmodel at each frame).

I hope this helps! Let me know if you have any questions or need further clarification.

2 Likes

Do you want one that’s physically simulated or constant? The physically simulated ones are usually based on the character movement and the constant ones are sort of like an animation (usually based on a sine wave).

1 Like

So i know i can use the root joint transform right but is there any way I can turn the root joints transform into an equation for procedural motion. Here is an example of what i want to turn into a trig function:

``````local x,y,z = rootJoint.Transform:toOrientation()
local Position = rootJoint.Transform.Position
viewmodelRootJoint.Transform:lerp(CFrame.new(Position) * CFrame.fromOrientation(0, y, z), 0.075 * DeltaTime * 60)
``````

What does your animation look like? If the torso is moving in a back and forth pattern then using the rootJoint’s transform would work.

You’ll also probably want to factor in other variables like movement speed and movement duration if your torso part isn’t moving back and forth.

What style of bobbing do you want? Do you want something like FPS games or something like runner games (ex: Evade).

For both games, you can do something using this smooth ramp up sine equation:

(Switch from red to blue around 7 pi)

The x variable is a combination of speed and duration (possibly the hrp torso offset too). The humanoid offset should go right on when the result is greater than 1 and left when the result is less than one. It should move down when the result is less than or greater than 0. You can plug the calculated offset into a spring module to smooth the values.

Some examples of an x values might be:

``````x = speed
x = speedGreaterThanZeroDuration * (speed/10)
``````

You can also add the bob from the character after. CloneTrooper’s camera bobbing comes from the head movement. You could make an additional spring for the head movement and sum the results to create your head bobbing.

Here is the spring module I use:

It’s made by Quenty. It can do numbers, Vector3s and Vector2s.

4 Likes

It looks like you are trying to create a procedural motion for a character in Roblox. To turn the root joint’s transform into an equation, you could try expressing it as a function of time, t.

One way to do this would be to use trigonometric functions to describe the motion. For example, you could use the sine function to describe oscillatory motion, or the cosine function to describe circular motion.

To use a trigonometric function to describe the motion of the root joint, you would need to express the position of the root joint as a function of time, t. This would involve specifying the amplitude, frequency, and phase of the motion.

Once you have described the motion of the root joint as a function of time, you can use this equation to drive the motion of the character. You can update the position of the root joint at regular intervals using this equation, and use the `lerp` function to smoothly interpolate between the current position and the target position.

To use a trigonometric function to describe the motion of the root joint in Roblox,

you can define a function that takes in the current time, t, as an input and returns the position of the root joint at that time.

Here is an example of how you could define such a function in Roblox code:

``````-- Define the amplitude, frequency, and phase of the motion
local amplitude = 1
local frequency = 1
local phase = 0

-- Define the function that describes the motion of the root joint
function getRootJointPosition(t)
-- Calculate the x, y, and z components of the position using trigonometric functions
local x = amplitude * math.sin(frequency * t + phase)
local y = amplitude * math.cos(frequency * t + phase)
local z = 0

-- Return the position as a Vector3
return Vector3.new(x, y, z)
end
``````

The OP is trying to turn the RootJoint’s CFrame offset into 1D values that can be plugged into a function to generate camera bobble. The code does the opposite of that, it turns a value into a root joint offset (also wrong type, should be CFrame not Vector3).

It still does the opposite. The function should take a CFrame and output a set of values (like velocity, velocity, velocity, right offset, etc).

Something like this:

``````local lastOffset = nil

function getValues(rootOffset)
local rightOffset = rootOffset.Position.X
local upOffset = rootOffset.Position.Y
local velocity = Vector.new(0, 0, 0)
if lastOffset then
velocity = Vector.new(rootOffset.Position.X / lastOffset.Position.X,
rootOffset.Position.Y / lastOffset.Position.Y,
rootOffset.Position.Z / lastOffset.Position.Z)
end
lastOffset = rootOffset
return rightOffset, upOffset, velocity
end
``````

Then plug a function of those values into a sine function.

Yes, it looks like the example code I provided describes a function that generates oscillatory motion for the root joint using the sine and cosine functions. To turn the root joint’s CFrame offset into linear values that can be plugged into a function to generate camera bobble, you could try expressing the CFrame offset as a function of time, t, in terms of the desired camera bobble motion.

For example, you could use the sine or cosine function to describe oscillatory motion, or you could use a linear function to describe a straight line motion. You would need to express the CFrame offset as a function of time, t, and specify the amplitude, frequency, and phase of the motion.

Once you have described the CFrame offset as a function of time, you can use this equation to update the CFrame of the root joint at regular intervals. You can use the lerp function to smoothly interpolate between the current CFrame and the target CFrame.

Here is an example of how you could define a function to generate camera bobble in Roblox code:

``````-- Define the amplitude, frequency, and phase of the motion
local amplitude = 1
local frequency = 1
local phase = 0

-- Define the function that generates camera bobble
function getCameraBobble(t)
-- Calculate the x, y, and z components of the CFrame offset using a trigonometric function
local x = amplitude * math.sin(frequency * t + phase)
local y = 0
local z = 0

-- Return the CFrame offset as a Vector3
return CFrame.new(x, y, z)
end`````````
2 Likes