Camera-Shake Effect in a FPS Game

Hi,

I have been wanting to write this post for a long time but as I was busy with my other work it just got delayed.

If you have played FPS games then you must have noticed some kind of effect which makes the user feel whenever his health is reducing. Such as the camera shake or the screen becoming red with blood splash. This kind of effect brings realism to the game and make the player feel the damage. If we don’t have any damage effect apart from just reducing the health count then somebody might be shooting the player from the back and the player would not even realise when he is dead. 

This is not a full detailed tutorial Article. This is for people who are familiar with Unity and who are looking for some advance steps in order to make their game good and exciting.

So following is the method which I have used to make a Camera Shake Effect. However there are many ways in which you can achieve that. This is the simplest ways which I could come up with.

As we are trying to shake the camera we will have to attach this Script to the main Camera of the character.

 

01> Make a Blank JavaScript and Name it CameraShake

02> Following is my Script. Please copy the script and I will explain in detail what each lines means.

===============================================================================

var cam : Transform;
var shakeAmount : float = 0.2;
var bringDownShake : float = 3;
var myTransform : Vector3;

static var shake : float = 0;

function Start()
 {
    myTransform = cam.transform.localPosition;
 }

function Update ()
{
    if(shake < 0)
    {
       shake = 0;
    }
    if(shake > 0)
    {
       cam.transform.localPosition = Random.insideUnitCircle * shakeAmount;
       shake -= Time.deltaTime * bringDownShake;
    }
    else
    {
       shake = 0;
       cam.transform.localPosition = myTransform;
    }
}

===============================================================================

03> Explanation:

 a> The first variable is a variable which will hold our camera GameObject. So make a variable cam and assign it to Transform as our camera will be moving.

 b> The Next variable is the shakeAmount which will determine how much our camera will shake. Assign it value 0.2 as we want the shake to be enough for the player to feel its presence but we also don’t want the player to go off-balance of the view.

 c> Next Variable is bringDownShake which will bring our stop our Camera Shake after allotted period of time.

 d> next variable is myTransform which we are making to store our default camera position. The reason we are doing so is because your will notice that after the shake effect, the camera position will change. It will be very less but over the time as you play, you will notice major change int he position so its best to restore the camera position back to its default position after the shake.

 e> The next variable is shake variable. This is the duration which we want our shake to happen. Lets make this variable static as we want to change the value of it as we are getting hit. So it has to be called from the other script. Enter 0 as a default value of the shake.

 f> We have our Start function now.

 g> myTransform is storing the default position of the camera at the starting of the game. Notice that we are using localPosition instead of just position. The reason of that is that we don’t want the value to be affected in real space but only for the local space of the camera.

 h> In the Update function, first we are telling the script that if the shake value is less than 0 then make the shake = 0

 i> Next we want is that if the Shake value is greater than 0 then we want out shake function to occur

 j> We will be changing the local position of the camera and we want it to change it randomly. There’s two-way by which you can do it. One is insideUnitCircle and the other is insideUnitSphere Both gives your he same effect however I think Sphere makes it shake more. Feel free to experiment with it. Generally what it does is it makes an imaginary circle/sphere around the camera and the camera shake around that radius.

 k> Now we want the shake value to reduce gradually so as not to make the shake effect for ever. For that we are adding Time.deltaTime and multiplying it by bringDownShake value

 l> Next we want to make sure that when nothing is happening with the player the shake value will change to 0 and also the camera will be resorted to its default value which is stored in myTransform variable.

 

04> Now save the script and attach it to your main camera and also drag and add the camera in the Inspector

05> Now the last thing which need to be done is, we need to add “CameraShake.shake = 3;” to our health script where we are taking damage. This is to make sure that shake only runs when the player is hit and not always.

 

So, thats it for the Camera Shake effect. I would try to make a Blood Splash effect next. Will write an Article when I am done.

If any of you need more explanation please feel free to ask and I will try to explain it.

Looking forward for the feedback.

 

  1. No trackbacks yet.

You must be logged in to post a comment.