This commit is contained in:
OliverS 2024-04-21 09:21:25 +02:00
commit e9cb1330bb
1 changed files with 35 additions and 0 deletions

View File

@ -4,6 +4,8 @@ using System.Collections;
public class CameraController : MonoBehaviour public class CameraController : MonoBehaviour
{ {
public static CameraController instance;
[SerializeField] private GameObject cam; [SerializeField] private GameObject cam;
public float scrollSpeed = 2.5f; public float scrollSpeed = 2.5f;
@ -15,8 +17,17 @@ public class CameraController : MonoBehaviour
private float timer; private float timer;
private void Awake()
{
if (instance == null)
instance = this;
}
private void Start() private void Start()
{ {
if (instance != this)
instance = this;
defaultMaxSpeed *= 0; defaultMaxSpeed *= 0;
} }
@ -56,4 +67,28 @@ public class CameraController : MonoBehaviour
if (!Input.GetMouseButton(1)) if (!Input.GetMouseButton(1))
defaultMaxSpeed = Vector2.LerpUnclamped(defaultMaxSpeed, Vector2.zero, curve.Evaluate(evalTime)); 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<CinemachineCameraOffset>().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<CinemachineCameraOffset>().m_Offset = new Vector3(x, y, originalOffset.z);
elapsed += Time.deltaTime;
yield return null;
}
cam.GetComponent<CinemachineCameraOffset>().m_Offset = originalOffset;
}
} }