Hi,
How is it possible to create a timer which begins from 0 when a keyboard key is pressed?
function Update(){
if ( Input.GetKeyDown ("s") )
{
//timer begins from 0 and increase to infinity
}
}
Hi,
How is it possible to create a timer which begins from 0 when a keyboard key is pressed?
function Update(){
if ( Input.GetKeyDown ("s") )
{
//timer begins from 0 and increase to infinity
}
}
var timerStarted : boolean = false;
private var timer : float = 0.0;
function Update()
{
if (Input.GetKeyDown("s"))
{
timerStarted = true;
}
if (timerStarted)
{
timer += Time.deltaTime;
}
}
Do you want the timer to count only when the key is being pressed? If so you would have to change the script above, if not the one above should work fine. If you want a C# example of the one above here it is:
using UnityEngine;
using System.Collections;
public class SCRIPTNAME : MonoBehaviour {
public bool enableTimer = false;
private float timerV = 0.0f;
void Update () {
if (Input.GetKeyDown(KeyCode.S))
enableTimer = true;
if (enableTimer)
timerV += Time.deltaTime;
}
}
Thanks guys, it worked!