MilkShake Main Page API Reference
Camera Setup
Shake Parameters Shake Presets
Shaking the Camera Direct Reference Static Methods
Updating Shakes at Runtime Shake Instances

MilkShake Documentation

Welcome to the MilkShake documentation! You can use the navigation bar on the left to scroll to a topic.


Camera Setup

MilkShake uses the camera's local position and rotation to apply shakes, and shakes are always done relative to (0,0,0) local position and rotation. This means that your camera will need to be contained in an empty container object, with its local position and rotation set to (0,0,0), as shown below:

If you are using scripts to move the camera around, your camera movement scripts should be on the conatiner object, instead of directly on the camera object.

Finally, once your camera objects are all set up, you can add the Shaker component to your Camera object by going to Add Component > MilkShake > Shaker.

The Shaker component has a single property Add To Global Shakers. Turn this on if you want the Shaker to be added to a static list of shakers, so that you can shake it using static methods rather than a direct reference (more on this later).


Shake Parameters

Before we can start shaking the camera, we need to understand Shake Parameters. Shake Parameters define the behavior of a shake; the shake type, strength and roughness, fading, and position and rotation influence:

  • Shake Type – Shakes come in 2 types:
    • One-Shot – The shake fades in and fades out automatically. This kind of shake is good for short shakes like explosions.
    • Sustained – The shake fades in and continues to shake until told to stop. This kind of shake is good for long shakes like earthquakes.
  • Strength – The intensity / magnitude of the shake.
  • Roughness – Lower roughness values are slower and smoother. Higher values are faster and noisier.
  • Fade In – The time, in seconds, for the shake to fade in.
  • Fade Out – The time, in seconds, for the shake to fade out. This is mainly used for One-Shot shakes.
  • Position Influence – How much influence the shake has on the position. This can have any value, even negative values and values greater than 1.
  • Rotation Influence – How much influence the shake has on the rotation. This can have any value, even negative values and values greater than 1. You may need to set this to high values (i.e. 10 or more) to see noticable rotation shake.

Shake Presets

Shake Presets provide a way to store Shake parameters as assets in your project. You can create a new Shake Preset by going to Assets > Create > MilkShake > Shake Preset. Shake Presets have all of the same parameters described above:


Shaking the Camera

Now we can start shaking the camera. You can either use a direct reference to shaker components or you can use static methods on the Shaker class.


Direct Reference

You can shake a camera using a direct reference to one or more Shaker components. You can create an inspector variable for the Shaker component, or get the reference another way. Once you have a reference, call the Shake method, where you pass in a Shake Parameters object. In the below example, a Shake Preset inspector field is used:

using UnityEngine;
using MilkShake;

public class ShakeDemo : MonoBehaviour
{
	//Inspector field for the Shaker component.
    public Shaker MyShaker;
    //Inspector field for a Shake Preset to use as the shake parameters.
    public ShakePreset ShakePreset;

    private void Start()
    {
    	//Shake using the shake preset's parameters.
        MyShaker.Shake(ShakePreset);
    }
}

Static Methods

You can shake all active shakers without direct references by calling the static ShakeAll method on the Shaker class. Make sure that the shakers you want to shake using the static methods have the Add To Global Shakers property checked.

using UnityEngine;
using MilkShake;

public class ShakeDemo : MonoBehaviour
{
    //Inspector field for a Shake Preset to use as the shake parameters.
    public ShakePreset ShakePreset;

    private void Start()
    {
    	//Shake all active shakers using the shake preset's parameters.
        Shaker.ShakeAll(ShakePreset);
    }
}

Updating Shakes at Runtime

It is possible to start, stop, and update shakes at runtime using Shake Instances. For One-Shot shakes it is usually not necessary to save and modify the shake instance since the shakes are usually short, but for sustained shakes it is necessary to save the shake instance so that you can stop the shake when needed.


Shake Instances

Shake Instances are objects that hold shake data and have various methods you can use to start, stop, and pause a shake. Below is an example showing how you can start and stop a shake as well as modify its strength:

using UnityEngine;
using MilkShake;

public class ShakeDemo : MonoBehaviour
{
	//Inspector field for the Shaker component.
    public Shaker MyShaker;
    //Inspector field for a Shake Preset to use as the shake parameters.
    public ShakePreset ShakePreset;

    //The saved shake instance we will be modifying
    private ShakeInstance myShakeInstance;

    private void Start()
    {
    	//The Shake method returns a Shake Instance that you can save and modify.
        myShakeInstance = Shaker.ShakeAll(ShakePreset);
    }

    private void Update()
    {
    	//Start the shake, with a fade-in time of 1 second.
    	if(Input.GetKeyDown(KeyCode.Q))
    		myShakeInstance.Start(1f);

    	//Stop the shake, with a fade-out time of 1 second. Also make sure the shake is not removed by the Shaker once it is fully stopped.
    	if(Input.GetKeyDown(KeyCode.E))
    		myShakeInstance.Stop(1f, false);

    	//Pause or unpause the shake, with a fade-out time of 1 second. Pausing will stop the shake from moving, but keep the offset position and rotation.
    	if(Input.GetKeyDown(KeyCode.P))
    		myShakeInstance.TogglePaused(1f);

    	//Increase the shake strength when holding the Plus key on the numpad.
    	if(Input.GetKey(KeyCode.KeypadPlus))
    		myShakeInstance.StrengthScale += Time.deltaTime;

    	//Decrease the shake strength when holding the Minus key on the numpad.
    	if(Input.GetKey(KeyCode.KeypadMinus))
    		myShakeInstance.StrengthScale -= Time.deltaTime;
    }
}