How does "Gun Sway" work? How would Recoil work? Any tips, ideas or suggestions?

I am currently creating a first person shooter and I’m just starting off and I was thinking about how roblox FPS games have this sway when you walk or run or just do any action that involves holding the gun like Phantom Forces or Arsenal or HEDGEROWS II where the guns have sway to them. I don’t know how a gun sway would even work and I have no idea other than a bad and impractical usage of interpolation.

I also don’t know how recoil would work either, I don’t have any practical idea and I need help on how something like that could be created with my gun.

I am asking for any tips or ideas and suggestions or anything at all of what I could try. Right now I have made a simple Viewmodel that has a camerapart that you dont have to weld everytime you move it since it welds itself automatically through a script and this camerapart moves to your camera’s cframe allowing you to view the arms and the gun.

I am fairly new to roblox studio and I’ve decided to make an FPS as another one of my starting projects and I don’t know how a gun sway would work so I’m asking for any tips, ideas, suggestions, and things I should know so that I can figure out what I could do and I would really appreciate it.

I am making an FPS game from scratch.

I have a link to a video here to show you what my progress looks like (its bad imo):

3 Likes

Try playing an animation when the gun is shot

Its been six months, uhh i think it fixed

Ok this should help -

Sway: Calculate Dot product between cameraCFrameLookVector and gunCFrameLookVector, and the lower the dot product is, the more angle rotation offset it should have, if the dot product is exactly 1, then the equation could make it have no offset rotation

Recoil: Try using springs to make it look perfect, but also use math.noise to make the patterns of rotation applied more random and more realistic.

Another method to make recoil extremely realistic without huge knowledge of any math concepts is to animate a camera part in blender, and then when you have the part movement perfected, on an Rstep loop:

  1. Find animated camera part’s travel from its default CF to find camera part’s offset
    (which is defaultCameraPartCF:inverse() * currentCameraPartCF)
  2. Apply that offset to the camera, although the camera will also need a reference CF to move back to every frame
  3. If the game is only first person, and does not have any third person at all, you can just cframe the camera directly to the animated camera part.

Using both of the first 2 steps should look like this:

local travel = defaultCamPartCF:inverse() * currentCamPartCF
camera.CFrame = referencePointCF * travel

Just for your info, I have not tested any of these methods yet, but I am sure they will work!

3 Likes

please send the link of the game hedgerows ||, i can’t find it!

It’s in closed beta, iirc every couple of months they’ll open up. You can search up videos of it to see what he’s talking about

You should look into sine waves. They’re the most commonly used way to do sway.

For recoil, if you’re too lazy to use springs, you can just make an animation and play it every time your gun is fired.

Did some research (aka googling) and found this piece of code for sway.

Original post: Whats the best way to make gun sway? - #5 by jaschutte

local mult = 1
function renderloop() --bind this camera render loop
local rotation = workspace.CurrentCamera.CFrame:toObjectSpace(lastCameraCF) --get cframe delta.
local x,y,z = rotation:ToOrientation() --I'm sure there are better ways to get rotation but this will work for now.
swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) --calculate the sway using SIN
gun.CFrame = gun.CFrame * swayOffset --apply the sway
lastCameraCF = workspace.CurrentCamera.CFrame --update the last cframe
end

code credit: jaschutte