fluentparrot fluentparrot - 29 days ago 8
C# Question

give a count down when mouse click stop

if (Input.GetMouseButtonUp(0))
{
tim -= Time.deltaTime;

if (tim < 0)
{
tim = 0;
}

int t = Mathf.FloorToInt(tim);
timer.text = "timer" + t.ToString();

}


i want to make a count down when i stop clinking mouse.and then need to stop when i click it.and then start to countdown when i do not click a mouse. this code makes a count down just when i click mouse.
please help me ...

Answer

Write this code in your update method:

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        //reset cooldown when click
        tim = 0;
    }
    else
    {
        //start countdown when not click
        tim -= Time.deltaTime;
        if (tim < 0)
        {
            tim = 0;
        }
    }
    //show timer
    int t = Mathf.FloorToInt(tim);
    timer.text = "timer" + t.ToString();
}
Comments