Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    4
  • comments
    0
  • views
    889

Different Ways Of Shaking Camera In Unity

Sign in to follow this  
Vivek Tank

1037 views

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 !

A great way to add some real feel to your game is camera shaking. It’s great for signifying the action and making your camera feel more real and part of game.

starting-gif-1.gif

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 try it with scripting first.

1. By scripting

First setup a scene like this in Unity.

by-scripting-scene-setup-image.thumb.png.ad1a16c491c45da14bd55f372f271007.png

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.

The code in the scripts is shown as below.

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;
        }
       
    }

In this script, I have used the coroutine concept for shaking the camera. If you don’t know how to use coroutine in Unity you can check this blog. Trust me you will be master in 10 Minutes.

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!

You can see the screen shaking .

This was simple and quick way to shake the camera.

But wait, It has some limitation. It doesn’t rotate the camera in any way and there is 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 way to go.

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.thumb.png.6118f8896ece2d50ce9bcb5dbd3c7394.png

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.

3199second-transition-in-shakeing-camera.thumb.png.f9f72a11fe020433ce6b02de92906f22.png

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

7363first-transition-in-shakeing-camera.thumb.png.dfa1d34358577be8cbcf9f034496b2ac.png

Now make empty object named ShakeWithAnimation and add ShakeWithAnimation Script to it and has a particle system in child to see it more effective.

ShakeWithAnimation.cs

    using UnityEngine;
    using System.Collections;
    public class ShakeWithAnim : MonoBehaviour
    {
        public Animator camAnim;
        public ParticleSystem explodeStar;
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                camAnim.SetTrigger("Shake");
                explodeStar.Play();
            }
        }
    }
Name Method/variable/coroutine Description
Update() Unity Callback Used for checking whether User Presses Key A
ParticleSystem explodeStar; variable To store reference of particle System
Animator camAnim; variable To store reference of Camera Animator
explodePartical.Play(); method To Play the particle system
camAnim.SetTrigger("Shake"); method To set Trigger for Camera Animation for Shaking.

Go to the Unity and play the scene and press A.

Oh Yeah!

The screen shook again so you learned one more way to shake the camera.

But wait, all condition are fulfilled but,

what about roughness?

Was there any way to control it using animation?

Nahhhh!

Then how can we do that?

Yes, I know the same question is there in your mind.

Let’s try the last way and check it out.

3. By using plugin

First make new scene in Unity.

Now go to asset store and download the plugin EZ Camera Shake (this is recommended by me as I use this and is very good but you can use any other plug in of your choice).

After finishing downloading EZ CAMERA SHAKE, import it to your scene. The folder EZCameraShake will be added in your scene.

Add script CameraShaker of EZCameraShake to main camera and set the variables as you want.

Now create empty object and name it ExplodeHeart.

Then give particle system in child for more effective look.

Now create and add ExplodeHeart script to it.

Add EZCameraShake namespace to your script.

ExplodeHeart.cs

    using UnityEngine;
    using System.Collections;
    using EZCameraShake;
    public class ExplodeHeart : MonoBehaviour
    {
        
        public ParticleSystem explodePartical;
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.H))
            {
                explodePartical.Play();
                CameraShaker.Instance.ShakeOnce(4f, 4f, 0.15f, 1f);
            }
        }
    }
Name Method/variable/coroutine Description
Update() Unity Callback Used for checking whether User Presses Key H
ParticleSystem explodePartical; variable To store reference of particle System
explodePartical.Play(); method To Play the particle system
CameraShaker.Instance.ShakeOnce(4f, 4f, 0.15f, 1f); method To call ShakeOnce Method of CameShaker Script Of EZCAmeShake passing magnitude, roughness, fadInTIme and fadeOutTime.

Now go to Unity and play the scene and press H.

Yeah! The screen shaking was so smoother than the both the other way, wasn’t it?

By this way you created your camera shake with the plugin. But wait, does this mean that the shaking camera with the script and animation was bad idea.

Nope, instead the last way would give more load on your memory while running so if you don’t need this smoother shaking and roughness controlled camera shake the scripting one is really quick and easy.

So you have to be wise while choosing any one of the methods of camera shaking in your game according to your requirement.

Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • What is your GameDev Story?

    In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

    (You must login to your GameDev.net account.)

  • Blog Entries

  • Similar Content

    • By snacktime
      Looking for feedback from someone who has done this, mainly just to confirm that my guestimates are not way off.  I know enough to be dangerous not my area of expertise.
      Trying to budget for a custom 3D skeletal animation system with cross fading and 2 layer blending.  Context is Unity.
      My thought is someone who has done it before could probably get the core features working in some form in a month.  But by the time you factor in everything like bugs, performance refactors, platform specific gotcha's, whatever, it's probably around a 6 month job to get something actually usable in game.
      Does that sound reasonable?
       
       
       
       
       
       
       
    • By Data7 Games
      Roles Required: Experienced Animator/Rigger
      My Role: Creative Director, Game Designer & Writer
      My Previous Projects: 2 small mobile games

      Team Size: 6 At The Moment (including me)
      Project Length: Estimated to be completed sometime in 2020/2021

      Project Description: Rift One is a half-life inspired FPS game, you play as mark maxin, a police detective who is mysteriously kidnapped by a secret organisation! They want you to enter a portal, to another world, or they will kill you if you don’t, but before you can, the lab is invaded! And you are forced to enter, in order to survive the crumbling lab.

      Responsibilities—
      - Create Animations as told by Project leads.
      - Polish Animations as told.
      - Working closely with other team members.

      Requirements—
      - At least a tiny bit of experience in game development
      - Ability to solve problems creatively and effectively
      - knowledge of Animation/Rigging, the unity engine & FPS Games such as Doom, half life etc.
      - Ability to Create Animation and rig
      - Strong communication required
      this is currently **A KICKSTARTER PROJECT** Meaning you won’t be paid until we are funded!
      we are currently planning and researching for our kickstarter.
      Email me at Data7games@gmail.com if interested
    • By RoKabium Games
      Which one of the 4 menus in SAMA is your favourite? 1, 2, 3 or 4?
    • By horror_man
      Hello, I'm currently searching for additional talented and passionate members for our team that's creating a small horror game.
       
      About the game: The game would be a small sci-fi/post-apocalyptic survival horror 3D game with FPS (First person shooter) mechanics and an original setting and story based in a book (which I'm writing) scene, where a group of prisoners are left behind in an abandoned underground facility. It would play similar to Dead Space combined with Penumbra and SCP: Secret Laboratory, with the option of playing solo or multiplayer.
       
      Engine that'd be used to create the game: Unity
       
      About me: I'm a music composer with 4 years of experience and I'm fairly new in this game development world, and I'm currently leading the team that'd be creating this beautiful and horrifying game. I decided that making the book which I'm writing into a game would be really cool, and I got more motivated about doing so some time ago when I got a bunch of expensive Unity assets for a very low price. However, I researched about how to do things right in game development so I reduced the scope of it as much as I could so that's why this game is really based in a scene of the book and not the entire thing. Also I'm currently learning how to use Unity and learning how to program.
       
      Our team right now consists of: Me (Game Designer, Creator, Music Composer, Writer), 3 3D Modelers, 2 Game Programmers, 1 Sound Effect Designer, 1 Concept Artist, 1 3D Animator and 1 Community Manager.
       
      Who am I looking for: We are looking for a talented and passionate programmer that's experienced with Unity and C#.
       
      Right now the game is in mid-early development and you can see more information about it and follow our progress in our game jolt page here: https://gamejolt.com/games/devilspunishment/391190 . We expect to finish some sort of prototype in 3 months from now.
       
      This is a contract rev-share position
       
      If you are interested in joining, contributing or have questions about the project then let's talk. You can message me in Discord: world_creator#9524
    • By mlamanna_music
      Not sure which of my audio packs to purchase? Now you can experience a selection of music and audio design from all my products for FREE with Game Audio Try Pack.

×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!