How To Add Camera Shake In Unity Tutorial
In this tutorial, you'll learn how to add a dynamic camera shake effect in Unity to enhance the impact of collisions and dramatic events in your game. We’ll walk you through creating and configuring a camera shake script, implementing it to trigger on collisions, and testing to ensure everything works perfectly. Whether you're new to Unity or looking to polish your game’s feel, this quick guide will help you integrate a compelling camera shake effect with ease.
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
Unity Projects - ON SALE!!!
1. Water Sort Puzzle 9000 Levels
2. Ball Sort Puzzle 9000 Levels
3. Sling Shot
=========================================================
Scripts:
CameraShake.cs
using UnityEngine;
public class CameraShake : MonoBehaviour
{
public float shakeDuration = 0.5f; // Default duration of the shake
public float shakeMagnitude = 0.5f; // Default magnitude of the shake
public float dampingSpeed = 1f; // How quickly the shake fades
private Vector3 initialPosition; // The initial position of the camera
private float currentShakeDuration; // Track current shake duration
private float currentShakeMagnitude; // Track current shake magnitude
void Start()
{
// Save the initial position of the camera
initialPosition = transform.position;
}
void Update()
{
if (currentShakeDuration > 0)
{
// Apply the shake effect
transform.position = initialPosition + Random.insideUnitSphere * currentShakeMagnitude;
// Reduce the shake duration
currentShakeDuration -= Time.deltaTime * dampingSpeed;
// If shake duration is complete, reset position and shake parameters
if (currentShakeDuration <= 0)
{
transform.position = initialPosition;
currentShakeDuration = 0f; // Reset current shake duration
}
}
}
// Public method to trigger the shake with default values
public void TriggerShake()
{
TriggerShake(shakeDuration, shakeMagnitude);
}
// Public method to trigger the shake with specified values
public void TriggerShake(float duration, float magnitude)
{
currentShakeDuration = duration;
currentShakeMagnitude = magnitude;
initialPosition = transform.position; // Ensure the shake is relative to the current position
}
}
----------------------------------------------------------------------------------------------------------------------------------------
CameraShakeOnCollision.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShakeOnCollision : MonoBehaviour
{
public float shakeDuration = 0.5f; // Default duration of the shake
public float shakeMagnitude = 0.5f; // Default magnitude of the shake
public float dampingSpeed = 1f; // How quickly the shake fades
private Vector3 initialPosition; // The initial position of the camera
private float currentShakeDuration; // Track current shake duration
private float currentShakeMagnitude; // Track current shake magnitude
void Start()
{
// Save the initial position of the camera
initialPosition = transform.position;
}
void Update()
{
if (currentShakeDuration > 0)
{
// Apply the shake effect
transform.position = initialPosition + Random.insideUnitSphere * currentShakeMagnitude;
// Reduce the shake duration
currentShakeDuration -= Time.deltaTime * dampingSpeed;
// If shake duration is complete, reset position and shake parameters
if (currentShakeDuration <= 0)
{
transform.position = initialPosition;
currentShakeDuration = 0f; // Reset current shake duration
}
}
}
// Public method to trigger the shake with default values
public void TriggerShake()
{
TriggerShake(shakeDuration, shakeMagnitude);
}
// Public method to trigger the shake with specified values
public void TriggerShake(float duration, float magnitude)
{
currentShakeDuration = duration;
currentShakeMagnitude = magnitude;
initialPosition = transform.position; // Ensure the shake is relative to the current position
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
CollisionShake.cs
using UnityEngine;
public class CollisionShake : MonoBehaviour
{
public CameraShakeOnCollision cameraShake; // Reference to the CameraShake script
public float shakeDuration = 0.5f; // Duration of the shake
public float shakeMagnitude = 0.5f; // Magnitude of the shake
public string targetTag = "Collider"; // Tag to check for collisions
void Start()
{
// Ensure cameraShake is not null; optional debug log to confirm
if (cameraShake == null)
{
Debug.LogWarning("CameraShake reference is missing. Please assign it in the Inspector.");
}
}
void OnCollisionEnter(Collision collision)
{
// Check if the collision is with an object tagged with the specified tag
if (collision.gameObject.CompareTag(targetTag))
{
// Log collision details for debugging
Debug.Log("Collision detected with: " + collision.gameObject.name);
// Trigger camera shake
if (cameraShake != null)
{
cameraShake.TriggerShake(shakeDuration, shakeMagnitude);
}
}
}
}