diff --git a/Assets/Scripts/Camera/CameraController.cs b/Assets/Scripts/Camera/CameraController.cs index cd3d250..dbaf129 100644 --- a/Assets/Scripts/Camera/CameraController.cs +++ b/Assets/Scripts/Camera/CameraController.cs @@ -4,6 +4,8 @@ using System.Collections; public class CameraController : MonoBehaviour { + public static CameraController instance; + [SerializeField] private GameObject cam; public float scrollSpeed = 2.5f; @@ -15,8 +17,17 @@ public class CameraController : MonoBehaviour private float timer; + private void Awake() + { + if (instance == null) + instance = this; + } + private void Start() { + if (instance != this) + instance = this; + defaultMaxSpeed *= 0; } @@ -56,4 +67,28 @@ public class CameraController : MonoBehaviour if (!Input.GetMouseButton(1)) defaultMaxSpeed = Vector2.LerpUnclamped(defaultMaxSpeed, Vector2.zero, curve.Evaluate(evalTime)); } + + public void ShakeCamera(float intensity, float duration) + { + StartCoroutine(DoShake(intensity, duration)); + } + + private IEnumerator DoShake(float intensity, float duration) + { + Vector3 originalOffset = cam.GetComponent().m_Offset; + float elapsed = 0.0f; + + while (elapsed < duration) + { + float x = Random.Range(-1f, 1f) * intensity; + float y = Random.Range(-1f, 1f) * intensity; + + cam.GetComponent().m_Offset = new Vector3(x, y, originalOffset.z); + + elapsed += Time.deltaTime; + yield return null; + } + + cam.GetComponent().m_Offset = originalOffset; + } }