49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Cinemachine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class ScreenShaker : MonoBehaviour {
|
|
CinemachineVirtualCamera cam;
|
|
CinemachineBasicMultiChannelPerlin noise;
|
|
Coroutine coroutine;
|
|
bool rumbleHeld;
|
|
|
|
void Awake() {
|
|
cam = GetComponent<CinemachineVirtualCamera>();
|
|
noise = cam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
|
|
}
|
|
|
|
public void Shake(float magnitude = 1f, float rumbleLowFreq = 0.25f, float rumbleHighFreq = 0.75f, float duration = 0.2f) {
|
|
if (coroutine is {})
|
|
StopCoroutine(coroutine);
|
|
|
|
coroutine = StartCoroutine(ShakeCoroutine(magnitude, rumbleLowFreq, rumbleHighFreq, duration));
|
|
}
|
|
|
|
public void StartHeldRumble(float rumbleLowFreq = 0.25f, float rumbleHighFreq = 0.75f) {
|
|
rumbleHeld = true;
|
|
Gamepad.current.SetMotorSpeeds(rumbleLowFreq, rumbleHighFreq);
|
|
}
|
|
|
|
public void StopHeldRumble() {
|
|
rumbleHeld = false;
|
|
Gamepad.current.ResetHaptics();
|
|
}
|
|
|
|
IEnumerator ShakeCoroutine(float magnitude, float rumbleLowFreq, float rumbleHighFreq, float duration) {
|
|
noise.m_AmplitudeGain = magnitude;
|
|
noise.m_FrequencyGain = 10f;
|
|
Gamepad.current.SetMotorSpeeds(rumbleLowFreq, rumbleHighFreq);
|
|
|
|
yield return new WaitForSecondsRealtime(duration);
|
|
|
|
noise.m_AmplitudeGain = 0f;
|
|
noise.m_FrequencyGain = 1f;
|
|
if (!rumbleHeld)
|
|
Gamepad.current.ResetHaptics();
|
|
}
|
|
|
|
}
|