3DTD/Assets/Scripts/Camera/CameraShake.cs

44 lines
1.3 KiB
C#

using System.Collections;
using Cinemachine;
using UnityEngine;
public class CameraShake : MonoBehaviour
{
public static CameraShake instance;
void Start()
{
if (instance == null)
instance = this;
}
public void ShakeCamera(float intensity, float duration)
{
StartCoroutine(DoShake(intensity, duration));
print("Shaking da booty");
}
private IEnumerator DoShake(float intensity, float duration)
{
var cam = CinemachineCore.Instance.GetActiveBrain(0).ActiveVirtualCamera.VirtualCameraGameObject.GetComponent<CinemachineCameraOffset>();
CinemachineCameraOffset cinemachineCameraOffset = cam.GetComponent<CinemachineCameraOffset>();
Vector3 originalOffset = cinemachineCameraOffset.m_Offset;
float elapsed = 0.0f;
while (elapsed < duration)
{
float x = Random.Range(-1f, 1f) * intensity;
float y = Random.Range(-1f, 1f) * intensity;
print("x: " + x + "y: " + y);
cam.GetComponent<CinemachineCameraOffset>().m_Offset = new Vector3(x, y, originalOffset.z);
elapsed += Time.deltaTime;
yield return null;
}
cam.GetComponent<CinemachineCameraOffset>().m_Offset = originalOffset;
}
}