- Home /
WaitForSeconds not waiting for specified time
Hey guys, in the below script I am changing the priority of my cinemachine vcam and then starting a coroutine.
void KillingCamera() {
guardKillingDollyCart.m_Speed=1f;
ActiveCamera=gameSettings.GetComponent<GameSettingsController>().GiveActiveCamera();
if(ActiveCamera!=null)
{
if(gameSettings.GetComponent<GameSettingsController>().tpp)
{
var vcam=ActiveCamera.GetComponent<CinemachineFreeLook>();
guardKillingCameraController.Priority=vcam.Priority+1;
}
else{
var vcam=ActiveCamera.GetComponent<CinemachineVirtualCamera>();
guardKillingCameraController.Priority=vcam.Priority+1;
}
}
StartCoroutine(TimeSlowDown());
}
The coroutine currently contains only one WaitForSeconds but I have to filll it with multiple of them to change the Time.timeScale to match my animation.
IEnumerator TimeSlowDown() {
yield return new WaitForSeconds(0.3f);
Time.timeScale=0.06f;
print("Time is slowed down");
}
The problem is that unity does not wait for 0.3 seconds and changes the value of timeScale almost immediately most of the time. Only once or twice in every five times does it wait 0.3 seconds.
I would be very glad if someone could help me solve the problem or tell me another way of waiting for specific time in scripts.
Thanks in advance.
Try replacing WaitForSeconds
with WaitForSecondsRealtime
.
I had already tried that but the problem remains same. I want to make my animation in slow motion after 0.3 seconds. Do you know any other way of doing that?
The thing is the slow motion works all the time if I pause the game just as the animation is about to start and then resume it. The slow motion then always starts after 0.3 seconds. Does this suggest anything to you to what the problem maybe?
Answer by DenisIsDenis · Jun 22, 2021 at 10:11 AM
In theory, the coroutine can be replaced with simple counters:
bool isStartTimer;
float timeToWait = 0.3f;
float timer;
void KillingCamera()
{
guardKillingDollyCart.m_Speed = 1f;
ActiveCamera = gameSettings.GetComponent<GameSettingsController>().GiveActiveCamera();
if (ActiveCamera != null)
{
if (gameSettings.GetComponent<GameSettingsController>().tpp)
{
var vcam = ActiveCamera.GetComponent<CinemachineFreeLook>();
guardKillingCameraController.Priority = vcam.Priority + 1;
}
else
{
var vcam = ActiveCamera.GetComponent<CinemachineVirtualCamera>();
guardKillingCameraController.Priority = vcam.Priority + 1;
}
}
isStartTimer = true;
}
void Update()
{
TimeSlowDown();
}
void TimeSlowDown()
{
if (!isStartTimer) { return; }
timer += Time.unscaledDeltaTime;
if (timer >= timeToWait)
{
timer = 0;
isStartTimer = false;
Time.timeScale = 0.06f;
print("Time is slowed down");
}
}
Your answer
Follow this Question
Related Questions
How to gradually increase difficulty. 1 Answer
Using coroutines to do attack patterns 1 Answer
Can specified frames of an audio track trigger events? 0 Answers
How to decrease gravityScale in period of time? 1 Answer
Mysteries of yield 1 Answer