After much Googleing for how to do a camera shaking effect in Unity, I decided to write my own small script to accomplish the effect. It combines both positional movement with rotational movement to simulate closely an actual camera shake.
The two key variables are:
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
var originPosition:Vector3; var originRotation:Quaternion; var shake_decay: float; var shake_intensity: float;; function OnGUI () { if (GUI.Button (Rect (20,40,80,20), "Shake")) { Shake(); } } 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 = .3; shake_decay = 0.002; } |
