Created
January 17, 2020 13:25
-
-
Save Streamweaver/e672c4abe3ac39342c62464c83fa3653 to your computer and use it in GitHub Desktop.
Simple Camera Shake. Attach to a camera and run as a coroutine.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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