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):
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:
Find animated camera part’s travel from its default CF to find camera part’s offset
(which is defaultCameraPartCF:inverse() * currentCameraPartCF)
Apply that offset to the camera, although the camera will also need a reference CF to move back to every frame
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 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