- Home /
Timer that stops at the end of the game
I am making a platform game but i want to make a timer that counts the time thil you hit a cube at the end, if you hit the cube the time stops.
how can i make this.
Check collision between the cube and the player and if collision there is, then stop the clock.
Answer by ByteSheep · Jan 31, 2012 at 09:04 PM
You can use Time.deltaTime to add to a float variable to create a timer and then simply stop adding to it as soon as you collide with the cube:
var Timer : float = 0;
var IncreaseTime : boolean = true;
function Update () {
if(IncreaseTime == true)
{
Timer += Time.deltaTime;
}
}
function OnCollisionEnter (Col : Collision) {
if(Col.gameObject.name == "Cube")
{
IncreaseTime = false;
}
}
Make sure that your Cube is name "Cube" in your hierarchy view or alternatively change Col.gameObject.name == "Cube" to whatever the name of the cube is.. You can also use tags. Hope this was useful :)
Answer by Posly · Jan 31, 2012 at 09:06 PM
Try this out:
var timer : int = 0; var hitCube : boolean = false; function Update () { //check's if the player hit the cube, if not it counts if(hitCube = false) { timer ++ } } //check's if the player hit the cube function OnTriggerEnter(hit : Collider) { if(hit.GameObject.Tag == "Finish Cube") { hitCube = true; } }That should work, just make sure to tag the cube as Finish Cube.
Not hundred percent sure if that code is going to make the timer go +1 every second since you're adding 1 to the timer variable each frame.. That will probably make the timer go a bit quick and the value can vary depending on the computers frame rate :)
Your answer
Follow this Question
Related Questions
How to stop a timer with GUI button? 1 Answer
Audio after Audio Loop 1 Answer
Race timer needs to stop on trigger. Help 1 Answer
Explosive like in COD 4? 1 Answer
Timer activates script 1 Answer