- Home /
Stop watch for 100 meter dash
Hey, a part of my game is a 100 meter dash and I need a stop watch for this part. The time should start counting when the player collides with the startline object and stop when the player hits the finishing line collider. In the Questionarea I've found the following code. It is counting, but only in the inspector. It does'nt stop atm. Any ideas how I can manage it? Greetings.
var Timer : float = 0; var IncreaseTime : boolean = true; var Start : GameObject; var Ziel : GameObject;
function Update () {
if(IncreaseTime == true)
{
Timer += Time.deltaTime;
}
}
function OnCollisionEnter (Col : Collision) {
if(Col.gameObject.name == "finishingline_cube")
{
Time.timeScale = 0;
}
}
Answer by Jonesy19 · Mar 31, 2015 at 04:26 PM
A few things... So, do you know if the "OnCollisionEnter" function is being called? You can throw a debug.log("Test"); statement into the "OnCollisionEnter" code to see if it is being called. The word "Test" should appear in the console if it is being called. If not, that is your problem. For some reason, it's not colliding. If it is being called, that's great, then you can put the same debug code into the if statement and check if your if statement is working.
Also, you have the variable "IncreaseTime", and you are using it in an if statement in the update function, yet you are not changing that value in your collision function. Change the
Time.timeScale = 0;
code to
IncreaseTime = false;
I had IncreaseTime = false;before, but I read somewhere that timeScale would be better. Doesnt work with both variants. ^^ And no. The statement isnt working. I think I will have a closer look. Thx
In that case, I would say your OnCollisionEnter code is not working properly. Is it a 2D program or 3D? If it's 2D, then it should be OnCollisionEnter2D. Is it set to trigger? In that case it should be OnTriggerEnter. Also, are you sure that the name of the finish line is "finishingline_cube"?
Just a few things to look at...
As a place to start, I would try the debug statement to see if it's being called.