Last active
May 7, 2021 14:05
-
-
Save kleberandrade/fe196d0c41fc0831f3d202c920754a47 to your computer and use it in GitHub Desktop.
Shake de camera
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 UnityEngine; | |
public class CameraShake : Singleton<CameraShake> | |
{ | |
public AnimationCurve m_MagnitudeCurve; | |
public void ShakeOnce(float duration, float magnitude) | |
{ | |
StartCoroutine(Shake(duration, magnitude)); | |
} | |
private IEnumerator Shake(float duration, float magnitude) | |
{ | |
Vector3 originalPosition = transform.position; | |
float elapsedTime = 0.0f; | |
while (elapsedTime < duration) | |
{ | |
Vector3 position = Random.insideUnitSphere * (magnitude * m_MagnitudeCurve.Evaluate(elapsedTime / duration)) * Time.deltaTime; | |
transform.position = originalPosition + position; | |
elapsedTime += Time.deltaTime; | |
yield return null; | |
} | |
transform.localPosition = originalPosition; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment