Train-of-Thought/Assets/Scripts/CameraShake.cs
Gerard Gascón 862afc9b7a init
2025-04-24 16:56:52 +02:00

38 lines
898 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour{
float shakeAmount = 0;
public static CameraShake instance;
void Awake(){
instance = this;
}
public void Shake(float amt, float length){
shakeAmount = amt;
InvokeRepeating("DoShake", 0, 0.01f);
Invoke("StopShake", length);
}
void DoShake(){
if (shakeAmount > 0){
Vector3 camPos = transform.position;
float offsetX = Random.value * shakeAmount * 2 - shakeAmount;
float offsetY = Random.value * shakeAmount * 2 - shakeAmount;
camPos.x += offsetX;
camPos.y += offsetY;
transform.position = camPos;
}
}
void StopShake(){
CancelInvoke("DoShake");
transform.localPosition = Vector3.zero;
}
}