Different Ways Of Shaking Camera In Unity

July 9, 2018
protect

Objective

The main objective of this blog post is to give you an idea about Camera Shake in Unity3D.

 

Are You looking for giving some real feel to your game ?

Do you want to add some screen shaking effect to your game ?

Then you are at the right place !

Camera Shaking is a great way to add some real feel to your game. It’s great for signifying the action and making your camera feel more real and part of game. Check out the example.

Shaking camera example

So, if you want to add camera shake in your game then you are at the right place.

In this blog, I will show you how to create your own camera shake effect in 3 ways.

Let’s start with scripting first.

1. By scripting

First setup a scene like this in Unity.

by scripting scene setup image

Create an empty game object named Explodestar and a particle system in child to get more effective look.

Create a script named Explodestar attached it to ExplodeStar gameobject to play particle system on playing game.

Create another script named CameraShake and attach it to camera you want to give the shaking effect.

CameraShake.cs


using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour
{
  


    public IEnumerator Shake(float duration, float magnitude)
    {
        Vector3 orignalPosition = transform.position;
        float elapsed = 0f;
        
        while (elapsed < duration)
        {
            float x = Random.Range(-1f, 1f) * magnitude;
            float y = Random.Range(-1f, 1f) * magnitude;

            transform.position = new Vector3(x, y, -10f);
            elapsed += Time.deltaTime;
            yield return 0;
        }
        transform.position = orignalPosition;
    }

   
}

Here, I have used Coroutine for shaking the camera. If you are not familiar with how to use coroutine in Unity. I recommend refer it first.

Name

Method/variable/coroutine

Description

Shake()

coroutine

Coroutine used for shaking camera

Vector3  orignalPosition

variable

To store the original position of camera

float elapsed

variable

To Store Elapsed time

float x

variable

To store random x value

float y

variable

To store random y value

ExplodeStar.cs


using UnityEngine;
using System.Collections;

public class ExplodeStar : MonoBehaviour
{
    public ParticleSystem explodePartical;
    public CameraShake cameraShake;
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            explodePartical.Play();
            StartCoroutine(cameraShake.Shake(0.15f, 0.4f));
        }
    }
} 

 

Name

Method/variable/coroutine

Description

Update()

Unity Callback

To check whether User Presses Key E

ParticleSystem explodePartical;

variable

To store reference of particle System

CameraShake cameraShake;

variable

To store reference of Camera

explodePartical.Play();

method

To Play the particle system

StartCoroutine(cameraShake.Shake(0.15f, 0.4f));

method

To start coroutine for Camera Shaking

Now go back to Unity and hit play button and press key (E).

Woooohooooo! Your screen is shaking.

This was simple and quick way to shake the camera.

But wait...

It has some limitation.

  • Doesn’t rotate the camera in any way

  • No way to control the roughness.

It just moves the camera every frame and using coroutine is bit clunky sometimes. So, if you want smoother camera shake this is not the proper way.

Let’s try it with another way using the animation and check if it fulfills our demands.

2. By Using Animation

First setup a scene like this in Unity.

by animation scene setup image

Add animator to main camera and create 2 animations idle and shake in that animator.

In ideal animation start recording and set the camera to its original position.

And in shake animation move position of camera on every two frame as you want to shake it.

Now go to animator and create a trigger shake.

Make transition from idle to shake and uncheck the ‘has exit time’ checkbox and set transition time to 1.

Second transition in shakeing camera

Now make transition from shake to idle and set transition time to 1 but don’t uncheck has exit time.

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Read More>>