From 380028dc133ff5c575b952c926cc5d3c3a20ce9d Mon Sep 17 00:00:00 2001 From: kimrdd Date: Sun, 21 Apr 2024 09:19:38 +0200 Subject: [PATCH] shakeinpro --- Assets/Scripts/Camera/CameraController.cs | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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; + } }