when OnMouseDown() is on the right timing, it triggers an event. How to achieve it?
Basically, I am planing to use co-routine and WaitForSeconds(waitTime); to count the beats. The player scores when he clicks the the gameObject right on the beat.
Let's Say the waitTime is 1 second, the script will run a function every 1 second to check if the player clicks the gameObject. It allows the player to have +/- 0.1 second error when hitting the button. How do I achieve this?
my idea is like the following, not sure if it is the best way to do so:
private bool rightOnTime = false;
private int score = 0;
IEnumerator CountBeats()
{
yield return new WaitForSeconds(0.9);
rightOnTime = ture;
yield return new WaitForSeconds(0.1);
rightOnTime = flase;
}
}
void On$$anonymous$$ouseDown()
{
if (rightOnTime)
{
score +=1;
}
}
$$anonymous$$oved this into the help room. It's for user-specific Qs that are only of interest to the person who posted them.
Answer by GiyomuGames · Aug 26, 2015 at 12:38 AM
It looks you are on the right track. Just put the content of your coroutine in a "while (true)" loop and your rightOnTime bool will behave correctly I believe.
Don't hesitate to mark it as the answer if you are satisfied ^^
Answer by MalachiteBR · Aug 26, 2015 at 12:54 AM
I would do way different than that. Maybe you record every clicks and the time it was made, and doing a very simple math you can extract how many clicks, the order and the time it took between 1 click and another.