Unity3D : Camera Shake Effect (script)

There is nice posted from Michael Jasper’s Web for how to do a camera shaking effect in Unity3d, It’s quite simple, it combines both positional movement with rotational movement to simulate ‘Shaking Effect’ to player. I modifying it a little bit so we can set it’s variable via inspector view. put this code bellow into js script and rename it to cameraShake.js

#pragma strict

var originPosition:Vector3;
var originRotation:Quaternion;

var shake_decay: float = 0.002;
var shake_intensity: float;
var coef_shake_intensity : float = 0.3f;

function Start () {

}

function Update(){
    if(shake_intensity > 0){
        transform.position = originPosition + Random.insideUnitSphere * shake_intensity;
        transform.rotation =  Quaternion(
                        originRotation.x + Random.Range(-shake_intensity,shake_intensity)*.2,
                        originRotation.y + Random.Range(-shake_intensity,shake_intensity)*.2,
                        originRotation.z + Random.Range(-shake_intensity,shake_intensity)*.2,
                        originRotation.w + Random.Range(-shake_intensity,shake_intensity)*.2);
        shake_intensity -= shake_decay;
    }
}

function Shake(){
    originPosition = transform.position;
    originRotation = transform.rotation;
    shake_intensity = coef_shake_intensity;
}

In that code there is two key variable to manage our shaking effect,
shake_intensity : Shake_intensity determines the initial intensity of the shaking — how much variance to allow in the camera position.
shake_decay : Shake_decay controls is the amount that shake_intensity is decremented each update. It determines if the shake is long or short.

Now we can call shake function whenever we want, just put this line inside your trigger event :

var cam = GameObject.Find("Main Camera").GetComponent("cameraShake") as cameraShake;
cam.Shake();

that’s it, here is a little example of camera shake, play this little shooter game, using arrow as direction and space to shoot enemy, shoot enemy until it’s destroyed and Camera will ‘SHAKING’


4 Opinion to this post

  1. Krystal says:

    I read a lot of interesting content here. Probably you spend a lot of time writing, i know how to save
    you a lot of work, there is an online tool
    that creates readable, SEO friendly posts in minutes, just type in google – laranitas free content source

  2. Great goods from you, man. I have understand your stuff previous to and you’re just extremely
    wonderful. I actually like what you’ve acquired here, certainly
    like what you are saying and the way in which you say it.
    You make it enjoyable and you still care for to keep it sensible.
    I can’t wait to read far more from you. This is really a wonderful
    website.

    Review my page: קופץ לכאן

  3. TB0Y298 says:

    This is great and usefull.
    But i need help. Could you please send me the project folder of the “example game” that is shown in the webplayer demo please??

    Im a developer of RWA
    Send it to my email here > tb0y298@hotmail.nl

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment moderation is enabled. Your comment may take some time to appear.