I want to start to run a timer value only when the if condition is true.
My problem is that Tim.tim is the value since game started. Somehow that value should set to zero to make the script work
if (onPlate == true)
{
timeLeft = Time.time;
if (timeLeft > 15)
{
//end game
}
}
Rather write : timeLeft = timeLeft + time.deltaTime;
agree but that doesn’t set my time back to zero.
I am looking for way when my FP controller stands on cube the timer start to run starting from 0 and when the FP leave the cube it is back to zero, when the FP goes back on it, the timer starts again to count up starting from zero.
With my current script: when the FP goes on the plate the value of the time is right away the same amount of the Time.time since the game started.
if (onPlate == true)
{
timeLeft = startTime + Time.time;
if (timeLeft > 15)
{
timeLeft = 0;
startTime = 0;
Application.LoadLevel("gameover");
}
}
else
{
timeLeft = 0;
startTime = 0;
}
}
On entering the plate
startTime = Time.time;
While Standing on the plate
elapsedTime = Time.time - startTime;
if (elapsedTime > 15)
{
Application.LoadLevel("gameover");
}
On leaving plate
elapsedTime = 0;
The elapsed time is the difference between the start time and the current time.
Where these blocks of code end up is very dependant on your code.
for while standing on the plate is this function correct?
function OnControllerColliderHit(hit:ControllerColliderHit){
if(hit.gameObject.tag == "targetplace"){
//startTime = Time.time;
onPlate = true;
print ("yes");
elapsedTime = Time.time - startTime;
if (elapsedTime > 15)
{
Application.LoadLevel("gameover");
}
else{
elapsedTime = 0;
}
Any advise I should use for “On entering the plate”
thanks for the help
c
var startTime : 180;
var minutes : int = 0;
var seconds : int = 300.0;
var fraction : int = 0;
var timeLeft = startTime;
var loadsceneGameOvr : String;
var timerActive : boolean = false;
function Start()
{
startTime= 180;
timeActive = false;
}
function Update () {
//The time left for player to complete level
timeLeft = startTime - Time.time;
//Don’t let the time left go below zero
timeLeft = Mathf.Max (0, timeLeft);
//Format the time nicely
guiText.text = FormatTime (timeLeft);
// Loads the level with index 0
if (timeLeft < 1.0) {
Application.LoadLevel (loadsceneGameOvr);
}
if (timerActive == true){
startTime -=1 * Time.deltaTime;
guiText.text = FormatTime(startTime);
}
//Format time like this
//12(minutes):34(seconds).5(fraction)
function FormatTime (time) {
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
var fraction : int = time * 10;
fraction = fraction % 10;
//Build string with format
//12(minutes):34(seconds).5(fraction)
timeText = minutes.ToString () + “:”;
timeText = timeText + seconds.ToString ();
timeText += “.” + fraction.ToString ();
return timeText;
}
This is what I apply in my text as a timer.
this is wat i apply in my character.
( i wanted the timer to start only when it hit the concrete floor. But it still starts although i am not on the concrete floor.) Can help?
Get rid of the “timeActive” on your timer script and instead have it reference Character.timeActive (only replace “Character” with whatever your character script name is)
var startTime = 300.0;
var minutes : int = 0;
var seconds : int = 300.0;
var fraction : int = 0;
var timeLeft = startTime;
var loadsceneGameOvr : String;
function Start()
{
startTime= 300;
timerStart.timeActive;
}
function Update () {
//The time left for player to complete level
timeLeft = startTime - Time.time;
//Don’t let the time left go below zero
timeLeft = Mathf.Max (0, timeLeft);
//Format the time nicely
guiText.text = FormatTime (timeLeft);
// Loads the level with index 0
if (timeLeft < 1.0) {
Application.LoadLevel (loadsceneGameOvr);
}
}
//Format time like this
//12(minutes):34(seconds).5(fraction)
function FormatTime (time) {
var intTime : int = time;
var minutes : int = intTime / 60;
var seconds : int = intTime % 60;
var fraction : int = time * 10;
fraction = fraction % 10;
//Build string with format
//12(minutes):34(seconds).5(fraction)
timeText = minutes.ToString () + “:”;
timeText = timeText + seconds.ToString ();
timeText += “.” + fraction.ToString ();
return timeText;
}
(I NAME THIS SCRIPT AS timerStart applied to my 3rd person character tag as Player)
u mean like that? But i have error saying expressions in statements must only be executed for their side-effects.
oxl1
December 10, 2010, 3:29pm
9
I think this is throwing the error :
timerStart.timeActive;
Shouldnt it be :
timerStart.timeActive = false; // true
–
oxl
Not quite, I meant to change your references to “timeActive” to point instead to “timerStart.timeActive”, like this:
function Start()
{
startTime = 300;
timerStart.timeActive = false;
}
.........
if (timerStart.timerActive == true)
{
startTime -=1 * Time.deltaTime;
guiText.text = FormatTime(startTime);
}
There is no error.
But the timer still start count down although my character haven hit on concrete floor.
Any other idea I can fix it?
Maybe put the timeActive check at the beginning of your update function?
FizixMan, the script worked when trigger on floor the timer only start…
But I have other problem occur when timer<0.1 then application load to game over scene…
when i wanted to restart the level… my timer does not reset the time…
Do you why?
Maybe set timeActive back to false?
if (timeLeft < 1.0) {
timerFPS.timeActive = false;
Application.LoadLevel (loadsceneGameOvr);
}
nope its not working… it does not reset to the startTime
I have another script that can reset time… i tried adjusting it to fit into script… dint work…
Here is the script i have found that will reset the timer…
var timerText : GUIText;
var roundTime = 90.0;
var timeLeft;
var timeFinish = 0;
var loadsceneGameOvr : String;
function Update () {
timeLeft = Mathf.FloorToInt(Time.time);
if(timeLeft > timeFinish)
{
roundTime -= 1;
timerText.text = roundTime.ToString();
}
if(roundTime < 1)
{
Application.LoadLevel(loadsceneGameOvr);
}
timeFinish = timeLeft;
}
ANy idea how to fix it into my previous script?
Oh, if you want to reset the time, maybe you can just reset the variables:
if (timeLeft < 1.0) {
timerFPS.timeActive = false;
startTime = 60.0;
minutes = 0;
seconds = 0;
fraction= 0;
timeLeft = startTime;
Application.LoadLevel (loadsceneGameOvr);
}