How to shake the virtual camera when time scale is 0?
Hi everyone,
I am having a problem with making time freeze and camera shake at the same time. Here are my time freezer script and shake virtual camera script, please help me with this problem.
public class Shaker : MonoBehaviour {
public static Shaker Instance{ get; private set;}
private CinemachineVirtualCamera virtualCamera;
private CinemachineBasicMultiChannelPerlin virtualCameraNoise;
private float shakeTime;
void Awake(){
Instance = this;
virtualCamera = GetComponent<CinemachineVirtualCamera>();
virtualCameraNoise = virtualCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
}
void Update(){
if(shakeTime > 0){
shakeTime -= Time.deltaTime;
if(shakeTime <= 0f){
virtualCameraNoise.m_AmplitudeGain = 0f;
virtualCameraNoise.m_FrequencyGain = 0f;
}
}
}
public void ShakeCamera(float intensity, float frequency, float time){
virtualCameraNoise.m_AmplitudeGain = intensity;
virtualCameraNoise.m_FrequencyGain = frequency;
shakeTime = time;
}
}
public class AttackFreeze : MonoBehaviour { [Range(0f, 1.5f)] public float duration = 1f; public bool _isFrozen = false; private float _pendingFreezeDuration = 0f;
// Update is called once per frame
void Update()
{
if(_pendingFreezeDuration > 0 && !_isFrozen){
StartCoroutine(DoFreeze());
}
}
public void Freeze(){
_pendingFreezeDuration = duration;
}
IEnumerator DoFreeze(){
_isFrozen = true;
var original = Time.timeScale;
Time.timeScale = 0f;
yield return new WaitForSecondsRealtime(duration);
Time.timeScale = original;
_pendingFreezeDuration = 0;
_isFrozen = false;
}
}
and here is how I call the method: attackFreeze.Freeze(); Shaker.Instance.ShakeCamera(2f,2f,.2f);
The situation right now is the camera shake only when the timescale returns to normal. I tried to use unscaledDeltatime and realTimeSinceStartUp but it still not work.
Your answer
Follow this Question
Related Questions
Milliseconds Timer Question 1 Answer
is it possible to reset Time.time 1 Answer
How to create a timer that only increases when if command is true? 1 Answer
20 minute countdown timer 2 Answers