Home

Awesome

GG Camera Shake

An asset for camera shake in Unity.

Features:

Watch video tutorial Thumbnail

Table of Contents

  1. Installation
  2. Usage
  3. Presets
  4. PerlinShake
  5. BounceShake
  6. KickShake
  7. Time Envelope
  8. Spatial Attenuation
  9. Writing Custom Shakes

Installation

  1. You can install the package via Package Manager using this URL: https://github.com/gasgiant/Camera-Shake.git#upm.

  2. Or you can download .unitypackage and import it into your project.

Usage

Setup

  1. Make the camera a child of another gameobject. When you want to move the camera move the parent.

  2. Add CameraShaker component to the camera gameobject.

By default CameraShaker works with its own transform. Alternatively, you can add CameraShaker to any gameobject you like and set it's cameraTransform field in inspector, or by calling CameraShaker.Instance.SetCameraTransform.

Using Presets

The simplest way to shake the camera is to use presets. Class CameraShakePresets allows to generate some common shake types with one line of code.

Call CameraShaker.Presets.Explosion2D to start a shake. Don't forget to add CameraShake namespace.

using UnityEngine;
using CameraShake;

public class MinimalExample : MonoBehaviour
{
    private void Explode()
    {
        // Something explodes ...

        CameraShaker.Presets.Explosion2D();
    }
}

Without Presets

If you need more options, than provided by presets, you need to create an instance of some shake class and pass it into the CameraShaker.Shake. There are three default shake classes: PerlinShake, BounceShake and KickShake. You can also write your own shakes. The example below is for PerlinShake.

using UnityEngine;
using CameraShake;

public class Gun : MonoBehaviour
{
    [SerializeField]
    PerlinShake.Params shakeParams;

    public void Shoot()
    {
        // Shooting ...

        CameraShaker.Shake(new PerlinShake(shakeParams));
    }
}

The constructor of PerlinShake takes an instance of PerlinShake.Params as an input. You can expose the parameters variable on some MonoBehaviour or ScriptableObject to tweak the parameters in the inspector.

shakeparams

Shakes can have more options in their constructors. For example you can pass position of the source of the explosion into the PerlinShake constructor, and the shake will change the strength depending on the distance from the source to the camera.

public class Grenade : MonoBehaviour
{
    public void Explode(float explosionStrength, PerlinShake.Params shakeParams)
    {
        CameraShaker.Shake(new PerlinShake(shakeParams, explosionStrength, transform.position));
    }
}

Presets

ShortShake3D
Suitable for short and snappy shakes in 3D. Rotates camera in all three axes. Uses BounceShake algorithm.

ParameterDescription
strengthStrength of the shake.
freqFrequency of shaking.
numBouncesNumber of vibrations before stop.

ShortShake2D
Suitable for short and snappy shakes in 2D. Moves camera in X and Y axes and rotates it in Z axis. Uses BounceShake algorithm.

ParameterDescription
positionStrengthStrength of motion in X and Y axes.
rotationStrengthStrength of rotation in Z axis.
freqFrequency of shaking.
numBouncesNumber of vibrations before stop.

Explosion3D
Suitable for longer and stronger shakes in 3D. Rotates camera in all three axes. Uses PerlinShake algorithm.

ParameterDescription
strengthStrength of the shake.
durationDuration of the shake.

Explosion2D
Suitable for longer and stronger shakes in 2D. Moves camera in X and Y axes and rotates it in Z axis. Uses PerlinShake algorithm.

ParameterDescription
positionStrengthStrength of motion in X and Y axes.
rotationStrengthStrength of rotation in Z axis.
durationDuration of the shake.

PerlinShake

PerlinShake combines layers of Perlin noise with different frequencies to create smooth and nuanced shake. Works better for longer shakes. For very short shakes consider using BounceShake.

Constructor

ParameterDescription
parametersParameters of the shake.
maxAmplitudeMaximum amplitude of the shake.
sourcePositionWorld position of the source of the shake.
manualStrengthControlfalsePlay shake once automatically.
trueManually control strength over time.

For more details on maxAmplitude and manualStrengthControl see Time Envelope.

Params

ParameterDescription
strengthStrength of the shake for each axis.
noiseModesLayers of Perlin noise with different frequencies.
envelopeStrength of the shake over time.
attenuationHow strength falls with distance from the shake source.

For more details on envelope see Time Envelope. For more details on attenuation see Spatial Attenuation.

BounceShake

BounceShake is useful for short and precise shakes. Unlike PerlinShake, it will provide reliable shake strength. Consider using PerlinShake for longer and stronger shakes.

Constructors

BounceShake (first overload)

ParameterDescription
parametersParameters of the shake.
initialDirectionInitial direction of the shake motion.
sourcePositionWorld position of the source of the shake.

BounceShake (second overload)

Creates BounceShake with random initial direction.

ParameterDescription
parametersParameters of the shake.
sourcePositionWorld position of the source of the shake.

Params

ParameterDescription
positionStrengthParameters of the shake.
rotationStrengthStrength of the shake for rotational axes.
axesMultiplierPreferred direction of shaking.
freqFrequency of shaking.
numBouncesNumber of vibrations before stop.
randomnessRandomness of motion.
attenuationHow strength falls with distance from the shake source.

For more details on attenuation see Spatial Attenuation.

KickShake

Makes one kick in specified direction. Useful for recoil.

Constructors

KickShake (first overload)

ParameterDescription
parametersParameters of the shake.
directionDirection of the kick.

KickShake (second overload)

Creates an instance of KickShake in the direction from the source to the camera.

ParameterDescription
parametersParameters of the shake.
sourcePositionWorld position of the source of the shake.
attenuateStrengthChange strength depending on distance from the camera?

Params

ParameterDescription
strengthStrength of the shake for each axis.
attackTimeHow long it takes to move forward.
attackCurveForward motion curve.
releaseTimeHow long it takes to move back.
releaseCurveBack motion curve.
attenuationHow strength falls with distance from the shake source.

For more details on attenuation see Spatial Attenuation.

Time Envelope

Class Envelope controls amplitude of the shake over time. It can work in two modes. In automatic mode it plays the shake ones with selected maxAmplitude.

In manual mode you can keep the reference to the PerlinShake and change amplitude whenever you like.

public class Vibrator : MonoBehaviour
{
    [SerializeField]
    PerlinShake.Params params;
    PerlinShake shake;

    private void Start()
    {
        shake = new PerlinShake(params);
        CameraShaker.Shake(shake);
    }

    public void Vibrate(float amplitude)
    {
        shake.AmplitudeController.SetTargetAmplitude(amplitude);
    }
}

EnvelopeParams

See interactive demonstration.

ParameterDescription
attackHow fast the amplitude increases.
sustainHow long in seconds the amplitude holds maximum value.
decayHow fast the amplitude decreases.
degreePower in which the amplitude is raised to get intensity.

Spatial Attenuation

Class Attenuator provides methods for changing strength and direction of the shake depending on position of the shake source relative to the camera.

StrengthAttenuationParams

See interactive demonstration.

ParameterDescription
clippingDistanceRadius in which shake doesn't lose strength.
falloffScaleHow fast strength falls with distance.
falloffDegreePower of the falloff function.
axesMultiplierContribution of each axis to distance. E. g. (1, 1, 0) for a 2D game in XY plane.

Writing Custom Shakes

CameraShaker works with any class that implements ICameraShake interface.

public interface ICameraShake
    {
        // Represents current position and rotation of the camera according to the shake.
        Displacement CurrentDisplacement { get; }

        // Shake system will dispose the shake on the first frame when this is true.
        bool IsFinished { get; }

        // CameraShaker calls this when the shake is added to the list of active shakes.
        void Initialize(Vector3 cameraPosition, Quaternion cameraRotation);

        // CameraShaker calls this every frame on active shakes.
        void Update(float deltaTime, Vector3 cameraPosition, Quaternion cameraRotation);
    }

Here is a basic example of a custom shake class.

public class VeryBadShake : ICameraShake
{
    readonly float intensity;
    readonly float duration;
    float time;

    public VeryBadShake(float intensity, float duration)
    {
        this.intensity = intensity;
        this.duration = duration;
    }

    public Displacement CurrentDisplacement { get; private set; }

    public bool IsFinished { get; private set; }

    public void Initialize(Vector3 cameraPosition, Quaternion cameraRotation)
    {
    }

    public void Update(float deltaTime, Vector3 cameraPosition, Quaternion cameraRotation)
    {
        time += deltaTime;
        if (time > duration)
            IsFinished = true;
        CurrentDisplacement = 
            new Displacement(Random.insideUnitCircle * intensity, Vector3.zero);
    }
}