Game Development
Screen shake
Learn to create impact with camera shake.
Screen Shake (Camera Shake)
Momentarily vibrating the camera during explosions, heavy blows, or critical hits (Screen Shake) conveys impact force directly to the player.
// Adding a random spherical offset to camera position
Vector3 shakeOffset = Random.insideUnitSphere * shakeMagnitude;
The Random.insideUnitSphere utility produces rapid displacements across random vectors, crafting an organic tremor.
Shake Duration and Coroutines
Screen shake should trigger instantaneously and dampen out smoothly. Running it inside a Coroutine applies jitter across a designated duration before restoring original placement.
IEnumerator Shake(float duration, float magnitude) {
Vector3 originalPos = transform.localPosition;
float elapsed = 0f;
while (elapsed < duration) {
transform.localPosition = originalPos + Random.insideUnitSphere * magnitude;
elapsed += Time.deltaTime;
yield return null;
}
transform.localPosition = originalPos;
}
This transient displacement delivers dynamic punch without corrupting baseline camera framing.
Exercise Camera shake magnitude test
Define a Start method. Inside, declare float magnitude = 0.3f; and print "Sarsinti siddeti: " + magnitude to the console.
Show example solution
using UnityEngine;
public class ScreenShakeTester : MonoBehaviour
{
void Start()
{
float magnitude = 0.3f;
Debug.Log("Sarsinti siddeti: " + magnitude);
}
}
Test yourself
-
Which random vector generator is commonly utilized to produce jitter for a Screen Shake effect?
- Vector3.one
- Quaternion.identity
- Random.insideUnitSphere
- Vector3.zero
Show answer
Random.insideUnitSphere — Random.insideUnitSphere returns a random 3D point inside a unit sphere, ideal for perturbing the camera position each frame.
-
Random shake displacement
transform.localPosition = originalPos + Random.____ * magnitude;
Show answer
transform.localPosition = originalPos + Random.insideUnitSphere * magnitude;
-
Camera shake terms
- Screen Shake
- Effect shaking camera on impact
- magnitude
- Amplitude and strength of the shake
- duration
- Duration in seconds the shake lasts
- originalPos
- Original position restored after shake
Next lesson: Hit feedback