Skip to content

Instantly share code, notes, and snippets.

@Streamweaver
Created January 17, 2020 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Streamweaver/e672c4abe3ac39342c62464c83fa3653 to your computer and use it in GitHub Desktop.
Save Streamweaver/e672c4abe3ac39342c62464c83fa3653 to your computer and use it in GitHub Desktop.
Simple Camera Shake. Attach to a camera and run as a coroutine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Simple camera shake. To call just attach to camera and from whever you want to
// call it, do something like 'StartCoroutine(_cameraShake.Shake(0.15f, 0.075f));'
public class CameraShake : MonoBehaviour
{
public IEnumerator Shake(float duration, float magnitude)
{
Vector3 orignalPosition = transform.position;
float elapsed = 0f;
while (elapsed < duration)
{
float x = Random.Range(-1f, 1f) * magnitude;
float y = Random.Range(-1f, 1f) * magnitude;
transform.position = new Vector3(x, y, -10f);
elapsed += Time.deltaTime;
yield return 0;
}
transform.position = orignalPosition;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment