Skip to content

Camera Shake and Weapon Recoil in Unity with Springs

A camera shake is a kick to the camera that wears off: it jumps a little, swings back past where it was, and settles. Done well it has three properties: it lands on the frame of the hit, it dies away smoothly rather than stopping, and a second hit during the first adds to it instead of restarting it. A spring gives all three on its own, which is why this page uses one.

The same three quick hits on the Chomper, the camera shaken by hand on the left and by a Transform Spring on the right. The trauma shake jitters and dies away. The spring kicks the view hard on each hit and snaps back with one small swing

Most cameras are moved every frame by something: a follow script, a cinematic rig, a player controller. A shake written into the camera’s own position is overwritten on the next frame, or fights the script that moves it. So give the camera an empty parent:

  • The parent is what your follow script moves.
  • The camera sits inside it at position 0,0,0 and rotation 0,0,0, and only the shake moves it, in local space.

The shake then rides on top of whatever the parent does, and when it ends the camera is back at 0,0,0 inside it. The VFX Toolkit’s own camera shaker is built the same way and expects the same set up (Screen Shaker and DoShake); on a camera with no parent it snaps the camera to the world’s 0,0,0.

The common way without an asset is a trauma value: each hit adds to it, it drains over time, and each frame the camera is offset by noise scaled by it. Squaring the trauma makes small hits subtle and big ones violent. Perlin noise, rather than a new random offset every frame, keeps the motion smooth instead of jittery.

using UnityEngine;
public class CameraShakeByHand : MonoBehaviour
{
[SerializeField] private float maxOffset = 1f;
[SerializeField] private float maxRoll = 6f;
[SerializeField] private float frequency = 25f;
[SerializeField] private float decay = 1.2f;
private float trauma;
public void AddTrauma(float amount) => trauma = Mathf.Clamp01(trauma + amount);
private void Update()
{
trauma = Mathf.Max(0f, trauma - decay * Time.deltaTime);
float shake = trauma * trauma;
float t = Time.time * frequency;
transform.localPosition = new Vector3(
(Mathf.PerlinNoise(t, 0f) * 2f - 1f) * maxOffset * shake,
(Mathf.PerlinNoise(0f, t) * 2f - 1f) * maxOffset * shake,
0f);
transform.localRotation = Quaternion.Euler(
0f, 0f, (Mathf.PerlinNoise(t, t) * 2f - 1f) * maxRoll * shake);
}
}

Put it on the camera, inside its parent, and call AddTrauma(0.5f) for a light hit and AddTrauma(1f) for a heavy one. The offsets are in metres, so these values suit a camera a few metres from the action; scale Max Offset with your scene.

The script on a camera a few metres from the Chomper, a light hit of 0.5 trauma on the left and a heavy one of 1 on the right. The light one is a short tremble, the heavy one a violent jolt that dies away

It works, and hits stack, because they add to one number. What it does not have is overshoot: the camera never swings back past its rest, which is the part that reads as a physical kick.

Cinemachine places the camera itself every frame, so a parent’s shake is lost and so is a shake on the camera. Use its own system instead: a Cinemachine Impulse Source on whatever causes the hit, which you fire with GenerateImpulse(), and a Cinemachine Impulse Listener on the Cinemachine camera, which turns those impulses into shake. It is the right tool when the project already runs Cinemachine.

Don’t own it yet? → Get it on the Unity Asset Store

Add a Transform Spring Component to the camera inside its parent (Add Component → AllIn1SpringsToolkit → Transform Spring Component), set Space Type to Local Space, and leave its target alone: the spring’s resting place is where the camera starts, 0,0,0 in its parent. Turn off the scale spring, which a camera does not need. Then hit it:

using AllIn1Assets.AllIn1SpringsToolkit;
using UnityEngine;
public class CameraKick : MonoBehaviour
{
[SerializeField] private TransformSpringComponent spring;
public void LightHit() => spring.RandomPunchPosition(18f);
public void HeavyHit() => StartCoroutine(spring.ShakePosition(magnitude: 25f, duration: 0.3f));
public void HitFrom(Vector3 worldDirection)
{
Vector3 local = transform.parent.InverseTransformDirection(worldDirection);
spring.PunchPosition(local, 20f);
}
}
Three cameras on the Chomper, the spring at Force 600 and Drag 40. RandomPunchPosition(18) kicks the view once and it snaps back. ShakePosition(25, 0.3) rattles it hard for a moment. Four punches 0.1 seconds apart keep it moving until the last one

The magnitudes are velocities in the spring’s space, tuned here for a camera a few metres from the action and the position spring set to Force 600 and Drag 40 in the component; try them, then scale them with your scene.

  • RandomPunchPosition shoves the camera once in a random direction across the screen. Called again before it settles, it adds to the motion that is already there, so rapid hits build up.
  • ShakePosition keeps punching for a duration, up to 40 punches a second and never more than one a frame (so at 30 fps a 0.3 second shake punches on 12 frames and lasts 0.4 seconds), each one turned away from the last so the camera shakes rather than drifts. Each punch resets the velocity first, so it does not stack with another shake running at the same time; stop the first with StopCoroutine, or use punches for hits that come in quick succession.
  • PunchPosition shoves along a direction you give, in the spring’s local space, so a hit from the left kicks the camera to the right.
  • ShakeRotation and RandomPunchRotation twist the camera around Z, a roll, which suits 2D cameras.

How the shake settles is the spring’s Force and Drag: more Force snaps back faster, more Drag swings back past less. The defaults, 150 and 10, swing back and forth three times over most of a second, which reads as wavy rather than as a hit; 600 and 40 hit their furthest point in two frames and settle in about a fifth of a second, with one small swing. A stiffer spring moves less for the same punch, so raise the magnitudes with it, as above. A SpringHitConfig gives one kind of hit its own Force and Drag. The whole list of punches and shakes is on Nudges and Punches, and Space Type on Transform Spring Component.

The shakes follow the component’s Use Scaled Time option, so with it off they keep shaking while the game is paused.

A Cam Fov Or Size Spring Component on the same camera adds a zoom punch: nudge its field of view, or its orthographic size in 2D, for a heavy landing or a big explosion.

Recoil is the same idea on the weapon. Parent the gun to an empty holder inside the camera, and put a Transform Spring Component in Local Space on the holder. On each shot, kick it back and tip the muzzle up:

using AllIn1Assets.AllIn1SpringsToolkit;
using UnityEngine;
public class Recoil : MonoBehaviour
{
[SerializeField] private TransformSpringComponent gunHolder;
[SerializeField] private TransformSpringComponent cameraSpring;
[SerializeField] private float kickBack = 2f;
[SerializeField] private float muzzleUp = 1f;
[SerializeField] private float sideways = 0.3f;
public void OnShot()
{
gunHolder.PunchPosition(Vector3.back, kickBack);
gunHolder.AddVelocityRotation(new Vector3(-muzzleUp, Random.Range(-sideways, sideways), 0f));
cameraSpring.AddVelocityRotation(new Vector3(-muzzleUp * 0.3f, 0f, 0f));
}
}
This Recoil script firing a burst of six. None of our demos has a gun, so the holder carries the 3D Shader demo's hover ship, seen from behind: each shot kicks it back and tips its nose up, the burst climbs, the camera kicks a little with it, and it all settles when the firing stops

AddVelocityRotation takes euler angles around the holder’s own axes: negative X tips the muzzle up. With muzzleUp at 1, the first shot tips it about 4 degrees and the burst builds to about 10. The toolkit scales the value internally, so start small and tune by eye. Because each shot adds to the motion already there, a burst climbs and then settles when the firing stops, with no recoil pattern to write. The smaller kick on the camera makes the shot felt in the view, not only seen on the gun.

Sway is the spring following, rather than being punched: each frame, set the holder’s target a little against the mouse or stick movement, and the gun lags behind the look and swings back into place.

Vector2 look = lookInput * swayAmount;
gunHolder.SetTargetPosition(restPosition + new Vector3(-look.x, -look.y, 0f));
The camera's parent turning left and right while the holder's target is set against the turn every frame: the ship lags behind the look and swings back into place

Knockback on a character, a spring on the body that the Animator keeps moving, is in Springs on an Animated Character.

  • Shake a Cinemachine camera. Cinemachine owns the camera’s position; use its Impulse system.
  • Stack two shakes. Each step of a running shake resets the velocity. Punches stack; shakes do not.
  • Shake along the camera’s depth. The punches and shakes move in X and Y, across the screen; a shove along Z needs PunchPosition with a direction that has one.