Question by
TamHartman · Sep 15, 2015 at 04:49 PM ·
guitimerguitexttimer countdowntimer-script
Problems showing accurate time with FixedUpdate()
Hey Guys, I'm working on this script that handles a countdown (3 seconds) until game start, the countdown for the duration of the game (30 seconds), and the game over sequence. So far it's working ok, but when I run it the time looks really janky. It's especially having problems switching from the pre-game countdown to the actual game countdown. Anyway, I'm wondering if you guys can help me get it working a little bit better, displaying the time more accurately.
public var GUIFontStyle : GUIStyle = new GUIStyle() ;
public var startObject : GameObject;
public var gameOver : GameObject;
public var scoreKeeper : scoreCollector;
public var starttime : float;
public var gametime : float = 30;
public var rounds : int = 1;
public var XPos = 200;
public var YPos : int = 0;
public var tWidth : int = 200;
public var tHeight : int = 20;
public var countdown : int = 3;
public var message : String = "Start";
function Start (){
starttime = Time.time;
}
function FixedUpdate(){
/*if (Time.time > gametime + starttime){
starttime = Time.time;
}*/
}
function OnGUI(){
var countDownTime : float = (starttime + countdown) - Time.time;
var timeleft : float = (starttime + gametime + countdown+1) - Time.time;
var display : float;
if (countDownTime > 1) {
display = countDownTime;
GUI.Label (Rect(XPos, YPos, tWidth, tHeight), display.ToString("0"),GUIFontStyle);
}
if (countDownTime < 1 && countDownTime > -1){
startObject.SetActive(true);
display = 30;
GUI.Label (Rect(XPos, YPos, tWidth, tHeight), display.ToString("0"),GUIFontStyle);
}
else if (timeleft <= 30){
if (timeleft >= 0){
startObject.SetActive(false);
display = timeleft;
GUI.Label (Rect(XPos, YPos, tWidth, tHeight), display.ToString("0"),GUIFontStyle);
}
if (timeleft < 0){
gameOver.SetActive(true);
}
}
//GUI.Label (Rect(XPos, YPos, tWidth, tHeight), display.ToString("0"),GUIFontStyle);
}
function newGame(){
gameOver.SetActive(false);
starttime = Time.time;
scoreKeeper.score = 0;
}
Comment
How about ?
function Update(){
if (Time.time > gametime + starttime){
starttime = Time.time;
}
}
FixedUpdate()
should be used for physics (Rigidbodies, AddForce etc....)
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.FixedUpdate.html
or if you really want to use FixedUpdate() consider using deltaTime
function FixedUpdate(){
if (Time.time > gametime + starttime){
starttime = Time.deltaTime;
}
}
or using a Corutine :
#pragma strict
public var GUIFontStyle : GUIStyle = new GUIStyle() ;
public var startObject : GameObject;
public var gameOver : GameObject;
public var score$$anonymous$$eeper : scoreCollector;
public var starttime : float;
public var gametime : float = 30;
public var rounds : int = 1;
public var XPos = 200;
public var YPos : int = 0;
public var tWidth : int = 200;
public var tHeight : int = 20;
public var countdown : int = 3;
public var message : String = "Start";
var firstCounter : float;
var secondCounter : float;
function Start (){
StartCoroutine(Timer(countdown, gametime));
}
function Timer(first : float, second : float){
while (first > 0) {
first -= Time.deltaTime;
GUI.Label (Rect(XPos, YPos, tWidth, tHeight), first.ToString("0"),GUIFontStyle);
firstCounter = first;
yield null;
}
Debug.Log("First Counter finished!");
yield WaitForSeconds(0.1);
while (second > 0) {
second -= Time.deltaTime;
GUI.Label (Rect(XPos, YPos, tWidth, tHeight), second.ToString("0"),GUIFontStyle);
secondCounter = second;
yield null;
}
Debug.Log("Second Counter finished!");
}
function OnGUI(){
if(firstCounter > 0.1){
GUI.Label (Rect(XPos, YPos, tWidth, tHeight), firstCounter.ToString("0"),GUIFontStyle);
}
if(secondCounter > 0){
GUI.Label (Rect(XPos, YPos, tWidth, tHeight), secondCounter.ToString("0"),GUIFontStyle);
}
}
function newGame(){
gameOver.SetActive(false);
starttime = Time.time;
score$$anonymous$$eeper.score = 0;
}