- Home /
Set timer to a specific time.
Hello, im a begginer javascript programer and i mad a game clock. it shows 24 real life minutes whic is a day in my future game. You can pause it set the morning time and the dawn time. Now i got two booleans, setToDayTime and setToNigthTime and i want these to set the clock to the morning time and the dawn time and continue the counting from there but i havent managed to make it in the past two days. Can someone help me out? #pragma strict
var mutato1 : GameObject;
var mutato2 : GameObject;
var mutato3 : GameObject;
var mutato4 : GameObject;
var dayTimeStartHour : int = 6; //morning start
var dayTimeEndHour : int = 18; //morning end
var setToDayTime : boolean = false; //set clock to morning start
var setToNigthTime : boolean = false; //set clock to morning end
var isItDayTime : boolean = false;
var pauseGameTime : boolean = false; //pause game time
var displayTime : float; //actual time displaying
var difference: float; // to pause game time
private var ido2 : float;
function aniSprite ( spriteObject : GameObject, columnSize : float, rowSize : float, type:String, index : int) // animating spirtes 0-9
{
var minute1 = index % 10;
var minute2 = (index/10) % 6;
var minute3 = (index / 60) % 10;
var minute4 = (index / 600) % 2.4;
if(type == "font1") index = minute1;
if(type == "font2") index = minute2;
if(type == "font3") index = minute3;
if(type == "font4") index = minute4;
var size : Vector2 = Vector2 ( 1.0 / columnSize, 1.0 / rowSize ); // find scale to show on poly
var offset : Vector2 = Vector2(index * size.x ,1);
spriteObject.renderer.material.mainTextureOffset = offset; // apply the offset amount to the correct sprite sheet object
spriteObject.renderer.material.mainTextureScale = size; // apply the scale amount to the correct sprite sheet object
}
function Update ()
{
var ido : float = Time.time - ido2;
if(!pauseGameTime) //pause gametime
{
displayTime = ido - difference;
ido -= difference;
}
if(pauseGameTime) //continue gametime
{
difference= ido - displayTime;
}
if( ido >= 1440) //reset timer from 1440 seconds (24min) to 0
{
ido2 = Time.time - difference;
}
if(displayTime >= dayTimeStartHour * 60 && displayTime <= dayTimeEndHour * 60)
{
isItDayTime = true;
}
else
{
isItDayTime = false;
}
if(setToDayTime)
{
displayTime = dayTimeStartHour * 60;
setToDayTime = false;
}
if(setToNigthTime)
{
displayTime = dayTimeEndHour * 60;
setToNigthTime = false;
}
aniSprite (mutato1, 10, 1, "font1", displayTime);
aniSprite (mutato2, 10, 1, "font2", displayTime);
aniSprite (mutato3, 10, 1, "font3", displayTime);
aniSprite (mutato4, 10, 1, "font4", displayTime);
}
Answer by Kiwasi · Oct 30, 2014 at 06:12 PM
Your structure is a bit of a nightmare. The class determining the time should have nothing to do with the various animations. You may find it easier if you remove all of the junk that is not related to keeping time to a different script.
Following your logic and variable names is a bit painful. The typical way to use a timer is to set a float to the time, then use a bunch of properties to convert that into the display time. There seems to be little point to keeping an offset to measure pause time, as this is automatically accounted for by using Time.time or Time.realTimeSinceStartUp. (Depending on which effect you are after).
Here is an alternative way to code a clock. (C#) Pseudo code, it doesn't do exactly the same as your class, but you get the idea.
float time;
public float hour {
get {return time/60/60 % 24;}
set {
time = value * 60 * 60;
}
}
// And so forth
void Update (){
time += Time.deltaTime; // or Time.unscaledDeltaTime
}
void SetMorning (){
hour = 6;
}
Thank you for your answer and for leading me to the rigth way. Im going to start the whole thing again with a clear $$anonymous$$d. Best wishes!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Adding force to a projectile? (SOLVED) 1 Answer
BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers
Script Not Working At All.... :) (SOLVED) 1 Answer
Errors with script using Javascript. 2 Answers