Hello everyone. I would like to give some insight to recoil theory which is essentially the building block to creating realistic gun systems.
First we need to understand something. In this theory, your mouse is essentially an area that the gun attempts to align with. Just like in real life, when you aim and fire, you try to bring the gun back to the center position(your eyes), in this case your mouse position. Doing so creates a more dynamic approach to creating recoil on weapons.
Let me introduce you to something called Virtual Pointer. Virtual pointer is really just your mouse position but emulated with offsets and smoothing. The gun aligns with your pointer, and your pointer is what you move when firing shots. This way your gun is not actually “recoiling”, but your hand/pointer position is moving.
Here is a small example of this theory in action → Video
I do accidently have some of my music playing, please ignore it…
To simply approach this theory we can utilize some small tools.
- Spring Library or any sort of smoothing
- UserInputService
Camera:ScreenPointToRay(x, y, depth)
CFrame.new(pos, lookAt)
- Virtual Pointer (Will show you a small code example)
Virtual Pointer
local UIS = game:GetService("UserInputService")
local Viewmodel = path.to.something
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter --// Only way this theory can work
local PointerOffset = Vector2.new(0,0) --// AKA recoil
local MousePosition = UIS:GetMouseLocation() + PointerOffset
local MouseRay = workspace.CurrentCamera:ScreenPointToRay(MousePosition.X, MousePosition.Y)
local CF = CFrame.new(Viewmodel.CFrame.Position, Viewmodel.CFrame.Position + MouseRay.Direction)
Viewmodel.CFrame = CF
Quick Note → You can apply the spring library onto the PointerOffset to create nice recoil without actually touching any CFrame.Angles!
Now if you do end up running this and creating a way to aim in. You’ll see if you change the PointerOffset you will still be looking down the barrel of the weapon! Lets say we wanted a little front nudge. Simply nudge the weapon a bit in a direction. E.g. Upwards
You can think of the pointer as your eyes/hands, the weapon will do its best to align with the eyes/hands.
Thank you for reading this! Hopefully this does spark some interest in any innovative studios, groups, or individuals.
Good Resources
– Deep Dive into Tarkov recoil mechanics
– Actual firearms being shot. (Cannot post that here because Roblox may not like that)