- Home /
How can i make my Timer script start and pause only when something certain is activated?
Hiya guise. I have a script here that works like a timer. And essentially i want it to only record when my NightVision is enabled (when you press N button), and when you disable nightvision the timer pauses, to start from where it was when you enable nightvision again etc. How can i go about doing this? Suggestions would be much appreciated!
CODE:
#pragma strict
private var startTime : float;
var textTime : String;
function Start ()
{
startTime = Time.time;
}
function OnGUI ()
{
var guiTime = Time.time - startTime;
var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("REC {0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
GetComponent(GUIText).text = textTime;
GetComponent (GUIText).color = Color.red;
GetComponent (GUIText).pixelOffset = Vector2 (-480, 250);
GetComponent (GUIText).fontSize = 25;
}
Answer by MonkeyHood · Jul 11, 2014 at 07:17 PM
Instead of " var guiTime = Time.time - startTime;", try adding Time.deltaTime.
deltaTime is time since last frame.
Also, I don't really know JS but the logic is sound. I'm more used to c#.
var time = 0; var timerOn = true; //this should be set to true or false when N is pressed
function OnGUI() { if(timerOn == true) time += Time.deltaTime; }
I dont really know how to implement it in my code, would be happy if someone could explain further. Good points though!
Answer by tconti · Jul 13, 2014 at 12:03 AM
you dont want to know startTime - current time what you want is to increase the timer during the Update() or onGUI() method JS isnt my strong suit but i hope i get my point across
function Start(){
timer = 0;
}
function onGUI () {
if(nightVisionIsActive){
timer += Time.deltaTime; //<--this is the amount of time since last frame
}
}
you need to keep track of when you are in night vision, so just toggle the bool in onKeyDown() then the timer will only count up when it is active
Feel free to edit in my code because im not that good with syntaxes yet.
Hi i edited my code to be able to make the timer stop when i press a button. But how come Time.time only works when its inside OnGUI and not when its declared as "x" ?? I mean that it doesnt start counting. Pls need some help!
CODE:
#pragma strict
private var startTime : float;
var REC : GameObject;
var textTime : String;
var x = Time.time;
function Start ()
{
REC.guiText.enabled = false;
startTime = 0;
}
function Update ()
{
if (Input.Get$$anonymous$$eyDown("n"))
{
if (REC.guiText.enabled == false)
{
REC.guiText.enabled = true;
}
else
{
REC.guiText.enabled = false;
}
}
}
function OnGUI ()
{
var guiTime = x;
var hours : int = guiTime / 3600;
var $$anonymous$$utes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("REC {0:00}:{1:00}:{2:00}:{3:00}", hours, $$anonymous$$utes, seconds, fraction);
GetComponent(GUIText).text = textTime;
}
Your answer
Follow this Question
Related Questions
switch game object on of by trigger with VR 0 Answers
GUIText not working properly with a timer. 1 Answer
Help with timer guitext 2 Answers
UI Button ON/Off 3 Answers