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.
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
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);
Can this method apply to the case that using smoothfollow script ?
Instead of shaking the screen, you can shake the camera. Here’s a similar question that might help your problem. Camera Shake Effect (Constant)? - Questions & Answers - Unity Discussions
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));
The snippet in this post works perfectly for me: Basic 2D “Screen Shake” in Unity. Like this post? You might like… | by Matt Buckley | Nice Things | iOS + Android Development | Medium
Here’s a short Youtube video that covers exactly how to code a screen shake effect: How to Code a Screen Shake Effect | Unity Tutorial - YouTube