- Home /
Screen Shake Effect
I want to make the screen shake whenever a certain explosion is triggered and I need suggestions or an example. The shaking needs to be somewhat random up or sideways.
Answer by buntu · Feb 05, 2011 at 07:13 AM
iTween has that kind of feature : http://itween.pixelplacement.com/index.php
And someone already asked for : http://answers.unity3d.com/questions/37345/itween-camera-shake
Answer by Owen-Reynolds · Mar 27, 2011 at 05:27 AM
This is pretty cheesy code, just adds randoms to Y each frame for a short while, but gives a decent effect with the right numbers. It assumes Update is computing transform.position each frame (if not, it will "walk" the camera):
// === camera script // globals: float jiggleAmt=0.0f; // how much to shake
// Update // Code setting transform.position....
// At end of update: if(jiggleAmt>0) { float quakeAmt = Random.value*jiggleAmt*2 - jiggleAmt; Vector3 pp = transform.position; pp.y+= quakeAmt; // can also add to x and/or z transform.position = pp; }
// Others call this to cause an earthquake: public void jiggleCam(float amt, float duration) { // Amt is how many meters the camera shakes. 0.5 is noticable jiggleAmt = amt; StartCoroutine(jiggleCam2(duration)); }
IEnumerator jiggleCam2(float duration) { yield return new WaitForSeconds(duration); jiggleAmt=0; }
// === Some other script: // Calling from other code: Camera.main.GetComponent<cam1Script>().jiggleCam(0.5f,0.75f);
Answer by lzt120 · Jan 19, 2013 at 12:22 PM
Can this method apply to the case that using smoothfollow script ?
Answer by littlehera · Oct 27, 2015 at 12:00 PM
Instead of shaking the screen, you can shake the camera. Here's a similar question that might help your problem. :) http://answers.unity3d.com/questions/755191/camera-shake-effect-constant.html
Answer by simulism · Jun 23, 2017 at 02:25 AM
I know this is old but, if you are using Itween, the code below will shake the screen when attached to the camera:
iTween.ShakePosition(gameObject, iTween.Hash("y", 0.7f, "time", 1.5f, "delay", 2.0f));
@simulism, your simple answer worked. I have tried for days trying to get a iTween camera shake while moving the camera to work. Your solution is the only one that stated that the ShakePosition logic had to be attached to the camera. I finally have my explosion working!!!