Course contentsShow
← Unity Game Development
Lesson 712 of 82810. VFX and Post-ProcessingFree lesson

Implementing Procedural Camera Shake for Impact

You will create a reusable camera shake script using Perlin noise to add tactile physical feedback to in-game events.

In this lesson, you will learn how to create a reusable camera shake system using Perlin noise to provide tactile physical feedback during intense gameplay events.

When a player fires a powerful weapon or suffers an explosion, static animations often feel stiff. To create organic, convincing movement, we use Perlin noise. Unlike pure random values, which result in jittery, "noisy" motion, Perlin noise generates a smooth, coherent flow of pseudo-random numbers. By sampling this noise over time and applying it to the camera's local position or rotation, we simulate the shaky, unstable feeling of physical impact.

Implementing the Shake Controller

The most efficient way to manage this is through a reusable CameraShake script that tracks a shakeDuration and shakeMagnitude.

public class CameraShake : MonoBehaviour
{
    private Vector3 originalPosition;

    public void TriggerShake(float duration, float magnitude)
    {
        StartCoroutine(ShakeRoutine(duration, magnitude));
    }

    private IEnumerator ShakeRoutine(float duration, float magnitude)
    {
        float elapsed = 0f;
        originalPosition = transform.localPosition;

        while (elapsed < duration)
        {
            float x = Mathf.PerlinNoise(Time.time * 20f, 0f) * 2 - 1;
            float y = Mathf.PerlinNoise(0f, Time.time * 20f) * 2 - 1;

            transform.localPosition = originalPosition + new Vector3(x, y, 0) * magnitude;
            elapsed += Time.deltaTime;
            yield return null;
        }

        transform.localPosition = originalPosition;
    }
}

In this implementation, we use Mathf.PerlinNoise to generate values between 0 and 1, then remap them to a range of -1 to 1 to allow for shake in both positive and negative directions. By multiplying these by magnitude and adding them to the originalPosition, we ensure the camera always returns to its intended state once the impact effect concludes.

Because this is procedural, you can easily tweak the frequency (by multiplying Time.time) to simulate different impacts—a low frequency for a heavy earthquake, or a high frequency for the rapid recoil of a submachine gun. Always ensure your camera is a child of a container object if you want to keep your base transform logic clean.

Key Takeaway: Use Perlin noise to generate smooth, organic motion trajectories for procedural camera shake, allowing you to simulate high-impact physical events without needing complex animations.