Screen shake
- Game Development course
- Advanced
- Lesson 62/71
- 4 min
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.
Practice Camera shake magnitude test
Define a Start method. Inside, declare float magnitude = 0.3f; and print "Sarsinti siddeti: " + magnitude to the console.
Starter code
using UnityEngine;
public class ScreenShakeTester : MonoBehaviour
{
// Start icinde magnitude tanimla ve yazdir
}Sample solution
using UnityEngine;
public class ScreenShakeTester : MonoBehaviour
{
void Start()
{
float magnitude = 0.3f;
Debug.Log("Sarsinti siddeti: " + magnitude);
}
}Expected output
Sarsinti siddeti: 0.3
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which random vector generator is commonly utilized to produce jitter for a Screen Shake effect?
- Vector3.one
- Quaternion.identity
- Random.insideUnitSphere Correct answer
- Vector3.zero
Explanation: Random.insideUnitSphere returns a random 3D point inside a unit sphere, ideal for perturbing the camera position each frame.
2 Random shake displacement
transform.localPosition = originalPos + Random.____ * magnitude;
transform.localPosition = originalPos + Random.insideUnitSphere * magnitude;
Explanation: In camera shake routines, 'Random.insideUnitSphere' provides a spherical random displacement vector.
3 Camera shake terms
| Term | Meaning |
|---|---|
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 |