- Home /
How to make timer pause for 3 seconds,How to make timer stop for 3 seconds when colliding with object?
So I want to make a timer powerup, I have the timer script already done but I don't know to make it pause for 3 seconds when colliding with an object, then we that 3 seconds is up the timer obviously resumes. Script works fine just want some code that can do what I explained. Would really appreciate it!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Timer : MonoBehaviour
{
public TMP_Text timerText;
public float startTime;
private void Start()
{
startTime = Time.time;
}
void Update()
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
}
}
You could add a function to your Timer script that triggers, when you collide with something. For example, when you collide, set cooldownTimer = 0f;
and make sure that it counts up (just like you do with the t
variable).
Now you capsule everything that is currently in your Update-function (line 19 to 24) into an if-statement, which checks if (cooldownTimer >= 3f){ // do the counting}
.
This is not a cook-book answer, but it only requires you to use mechanics that you already used in your original code. If you have further questions, just ask. Happy coding!
Ill give that a go in the morning its really late here thank you ill let you know how it goes!
Answer by Vivien_Lynn · Aug 12, 2021 at 03:51 PM
Here one solution, based on your code.
We keep track of a cooldown timer, which we can reset by calling AddCooldown()
.
Then we only keep counting with the "main timer", when there is currently no cooldown applied.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class MyTimer : MonoBehaviour
{
[Header("Timer in m:s")]
[Tooltip("Time elapsed since game start")]
[SerializeField] private string timeElapsed;
private float startTime;
[Header("Cooldown in s")]
[Tooltip("Time in seconds for which the timer stops counting")]
[SerializeField] private float cooldown = 3f;
private float cooldownTimer;
private void Start()
{
startTime = Time.time;
// Makes sure the cooldown does not stop the main timer right after start
cooldownTimer = cooldown;
}
void Update()
{
// For Testing. Press V to pause the Timer for 3 seconds
if (Input.GetKeyDown(KeyCode.V))
{
AddColldown();
}
// Count time since AddCooldown() was triggered
cooldownTimer += Time.deltaTime;
// Lets the main timer only count, if there is no cooldown running
if (cooldownTimer >= cooldown)
{
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timeElapsed = minutes + ":" + seconds;
}
}
private void AddColldown()
{
cooldownTimer = 0f;
}
}
I kind of understand thanks! I will have to create and ontrigger function onto and object though I assume.
Ah so its kind of working except when I pause the timer it never resumes after 3 seconds. Here is how I put your script into my one.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;
public class Timer : MonoBehaviour { [Header("Timer in m:s")] [Tooltip("Time elapsed since game start")] [SerializeField] private string timeElapsed;
[Header("Cooldown in s")]
[Tooltip("Time in seconds for which the timer stops counting")]
[SerializeField] private float cooldown = 3f;
public TMP_Text timerText;
public float startTime;
private float cooldownTimer;
private void Start()
{
startTime = Time.time;
cooldownTimer = cooldown;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
AddCooldown();
}
if (cooldownTimer >= cooldown)
{
cooldownTimer += Time.deltaTime;
float t = Time.time - startTime;
string $$anonymous$$utes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = $$anonymous$$utes + ":" + seconds;
}
}
private void AddCooldown()
{
cooldownTimer = 0f;
}
}
You did put cooldownTimer += Time.deltaTime;
inside of the if-statement, but you have to put it in front of the if-statement.
Ah I see it worked! Thank you very much for the help
Your answer
![](https://koobas.hobune.stream/wayback/20220613041543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a fill take exactly n seconds to complete 2 Answers
More timer from same class 1 Answer
CountDownTimer Help 1 Answer