Difficulty figuring out a certain kind of gun sway

i am making a gun system and am trying to recreate a kind of gun sway used in Deadline, an fps game on roblox. this kind of gun sway sort of adds the camera movement to a value permanently with limits instead of just having the gun sway with camera movement, if that makes sense.

here is the code of what i have come up with so far:

local mouseVector = InputService:GetMouseDelta()/100

sysValues.cameraPositionX += mouseVector.X/5
sysValues.cameraPositionY -= mouseVector.Y/5

local cposXLimit = sysValues.cameraPositionLimit
local cposYLimit = sysValues.cameraPositionLimit

if sysValues.cameraPositionX > cposXLimit then
	sysValues.cameraPositionX = cposXLimit-0.002
elseif sysValues.cameraPositionX < -cposXLimit then
	sysValues.cameraPositionX = -cposXLimit+0.002
end

if sysValues.cameraPositionY > cposYLimit then
	sysValues.cameraPositionY = cposYLimit-0.002
elseif sysValues.cameraPositionY < -cposYLimit then
	sysValues.cameraPositionY = -cposYLimit+0.002
end

and i just add these cameraPosition values to the cframe of the viewmodel.

this works but has a problem. the movement is limited to a box shape, which feels unnatural.

and i want it to be limited to a circle, like this

i have tried some simple stuff with the values but i am not great at math, so i can not come up with a solution. any help is appreciated. thank you

1 Like

Think this gun error maybe its not working

Is sysValues.cameraPosition where the gun’s muzzle is located?

cameraPosition is the value that is added to the cframe of the viewmodel. sorry, i forgot to explain the values so it must be pretty unclear to read

You can try getting the Unit vector and subtracting.

Code:

local mouseVector = InputService:GetMouseDelta().Unit
sysValues.cameraPositionX -= mouseVector.X
sysValues.cameraPositionY -= mouseVector.Y

local cposXLimit = sysValues.cameraPositionLimit
local cposYLimit = sysValues.cameraPositionLimit

if sysValues.cameraPositionX > cposXLimit then
	sysValues.cameraPositionX = cposXLimit-0.002
elseif sysValues.cameraPositionX < -cposXLimit then
	sysValues.cameraPositionX = -cposXLimit+0.002
end

if sysValues.cameraPositionY > cposYLimit then
	sysValues.cameraPositionY = cposYLimit-0.002
elseif sysValues.cameraPositionY < -cposYLimit then
	sysValues.cameraPositionY = -cposYLimit+0.002
end

This would be easier to achieve if we could set the position of the ViewModel directly though.