Extremely strange camera shake

I am attempting to add a camera shake to my current games weapons. I have made it so whenever the weapon is fired a shake function is activated which shakes the players camera, though I have ran into a strange problem where if the camera shake is called before it ends it will cause a stray bullet to appear (mostly happens when the weapon is automatic). This stray bullet is an outlier to where the bullet should land. This does not happen when the spread function is taken away. Here is how the shake is created and called:

function camShake()
	local Intensity = weaponSettings.cameraShake.Value
	for i = 1, 10 do
		local cam_rot = Camera.CoordinateFrame - Camera.CoordinateFrame.p
		local cam_scroll = (Camera.CoordinateFrame.p - Camera.Focus.p).magnitude
		local ncf = CFrame.new(Camera.Focus.p)*cam_rot*CFrame.fromEulerAnglesXYZ((-Intensity+(math.random()*(Intensity*2)))/100, (-Intensity+(math.random()*(Intensity*2)))/100, 0)
		Camera.CoordinateFrame = ncf*CFrame.new(0, cam_scroll, 0)
		wait()
	end
end


function FireBullet()
RemoteStorage.fireEvent:FireServer(Part, Position, Distance, Mouse.Hit.p, Weapon ,ray, bulletSettings.bulletDamage.Value, bulletSettings.bulletSpread.Value, weaponSettings.magNumber.Value, weaponSettings.reloadTime.Value, bulletSettings.bulletSpeed.Value) --fires raycasting information to server
	spawn(camShake) --spawns the shake event
end

Here is a gif of the weapons with and without the shake function:

With Camera shake:
https://gyazo.com/f5d7be2a6e1d20e7c677794b5daca99c

Without Camera shake:
https://gyazo.com/1042489d2b867cdbd294f0e0c76c5d9a

Im pretty sure this has to do with the buildup of many camera shake functions at once but I unsure. Could anyone help?

Do not use spawn function, I believe it has a sight delay. Use coroutine.wrap() instead.

1 Like

I’d recommend using springs for camera shakes since It’s by far one of the nicest ways to do this sort of thing.

Check out Quenty’s spring: https://github.com/Quenty/NevermoreEngine/blob/version2/Modules/Shared/Physics/Spring.lua

local Spring = require(Path.To.Spring)

local shakeSpring = Spring.new(Vector3.new())
shakeSpring.Speed = 16 -- for faster effects
shakeSpring.Damping = 0.5 -- basically how much "springyness" it has

RunService.RenderStepped:Connect(function(deltaTime) -- we're using deltatime to calculate how fast the camera changes
	shakeSpring:TimeSkip(deltaTime) -- this fowards the spring motion
	camera.CFrame = camera.CFrame * CFrame.Angles(shakeSpring.Value.X, shakeSpring.Value.Y, shakeSpring.Value.Z)
end)

shakeSpring:Impulse(Vector3.new(10, 2, 5)) -- play around with this, this basically creates the spring motion.  like when you flick one of those doorstoppers.

Feel free to ask me if you have any questions. I should mention that you should use rotation for camera shakes since that generally looks much cleaner

4 Likes

This worked perfectly and it looks so much better. Thank you.

I’ve had this problem before with with functions, thanks for giving me a fix for this problem.