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.
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.
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
Time.timeScale = 0; => This function will stop Time.
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.