How do i do a gun sway/ Whats the best way of doing it?

  1. What do you want to achieve? Keep it simple and clear!

Basicly I am wondering how i do the gun sway part. Any tips or ideas will work.

i am not asking for full scripts i am just wondering on how to and tips

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Solutions i have tried is not many either they will fling the character or so. I have been looking on here but all I find is something idk what they mean or wanna bind it to.

Progress: Sorry for bad quality
https://gyazo.com/f0d34b14285aee8f29219222c5b542d5

1 Like

So I haven’t actually made gun sway before so if this doesn’t work sorry, but couldn’t you use animation?

No not what i have been reading and understanding u cant use animation to the the gun sway

Oh ok, ya sry i havent messed with this stuff

a lot of games use spring to accomplish gunsway, but you can also do it using sine waves:tm: and lerping between the last camera cframe to the current one, here’s a code snippet, comes with idle sway as a bonus just because it’s easier for me to keep it there than forking it out

local lastCF = cam.CFrame
local sway = CFrame.new()
local sinValue = 0
local lerp = 0
game:GetService("RunService").RenderStepped:Connect(function(dt)
	local deltaRatio = deltaFramerate / (1 / dt) --//deltaFramerate is the framerate you want things to update in, 60 is usually a good pick
	
	local function calculateSine(speed) --//for idle sway
		sinValue += speed * deltaRatio
		if sinValue > (math.pi * 2) then sinValue = 0 end
		local sineY = 0.01 * math.sin(2 * sinValue)
		local sineZ = 0.01 * math.sin(sinValue)
		local sineCFrame = CFrame.new(sineZ, sineY, 0)
		return sineCFrame
	end
	
	local swaymultiplier = 0.3 --//sway speed
	local x, y, z = workspace.Camera.CFrame:toObjectSpace(lastCF):ToOrientation() 
	sway = sway:Lerp(CFrame.Angles(math.sin(x) * swaymultiplier,math.sin(y) * swaymultiplier, 0), 0.15)  --//for gun sway
	
	lerp -= 0.1 / aimTime * deltaRatio
	if lerp < 0 then lerp = 0 end
	local sineCFrame = calculateSine(0.04)
	Weapon.Aim.CFrame = (cam.CFrame * restPosition * sineCFrame):lerp(cam.CFrame * aimPosition, lerp) * sway
	lastCF = cam.CFrame
end)

this likely won’t be compatible with your current framework so you’ll have to do some adjustments

3 Likes

Ok will try but the restposition is a CF ? and the aimposition?

and

weapon.Aim.Cframe

is that the aim or something else

it’s my method of moving the viewmodel, same with rest position and aim position, you can simplify that part as

	local sineCFrame = calculateSine(0.04)
	viewmodel cframe = (cam.CFrame * restCFrame * sineCFrame) * sway
	lastCF = cam.CFrame

Ok so the veiwmodel CFrame is the primary part of the view model?
And the rest position is the same as where i the regular for CFrame is for the primarypart

1 Like

Can somone help still i cant get this gun sway to work

this actually works


image
https://gyazo.com/ea41458d8bda8bdc64c9e520e1ddbbe6

1 Like

I think it is possible watch Quake II gameplay and if they move the camera it looks like ID Software used weapon sway animations based on which way you move the camera (sorry for necrobump)

Late reply but

deltaFramerate / (1 / dt) is just

deltaFramerate * dt

I just get the mouse delta and make a few calculations and rotate the viewmodel based on those calcs.