Skip to content

Instantly share code, notes, and snippets.

@kleberandrade
Last active May 7, 2021 14:05
Show Gist options
  • Save kleberandrade/fe196d0c41fc0831f3d202c920754a47 to your computer and use it in GitHub Desktop.
Save kleberandrade/fe196d0c41fc0831f3d202c920754a47 to your computer and use it in GitHub Desktop.
Shake de camera
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