1
\$\begingroup\$

I have made a 2D Game using unity in which user has to select objects of similar color and now i want to display hint when user is unable to select the color for more than four seconds.

I want something similar to candy crush hint displaying system in which candy crush shows hint by highlighting the possible combination if user is not able to identify any combination himself.

I cannot figure out how to find if the user is inactive so that i can display hints.

I would really appreciate if someone can help me in figuring it out. Thanks in advance.

\$\endgroup\$
4
\$\begingroup\$

Whenever an action counting as active interaction (tap/successful match) happens, capture current time to a variable in the hint showing script. In Update() method check if the difference is greater than delay(seconds).

public class HintController : MonoBehaviour {
  public float LastTime;
  public float HintDelay = 5.0f;

  void Start() {
     LastTime = Time.realtimeSinceStartup;
  }

  void Update() {
     if(Time.realtimeSinceStartup - LastTime > HintDelay)
        displayHint();
  }
}

//...when/where tap/match/whatever logic happens, save current time
//hc is reference to the HintController script
//you can assign it, for example, through designer variable or getComponent<> method
hc.LastTime = Time.realtimeSinceStartup; 
\$\endgroup\$
2
\$\begingroup\$

Adding to wondra's solution, you can use OnMouseDown() in Unity to detect both mouse and tap events. So if you were to use:

void OnMouseDown()
{
    hc.LastTime = Time.realtimeSinceStartup;
}

Your timer would reset every time your user taps the screen or, on desktop, clicks the mouse. You can also add to this by adding a collider to the board (or whatever object is containing the candies) and using a Raycast to detect whether or not the tap/click is on the board. That way, any taps/clicks that are NOT on the board won't reset the hint timer.

\$\endgroup\$
1
\$\begingroup\$

Keep a Boolean if player is tapping or touching at the moment.Touch detection

If player is not touching the screen start a timer. Timer with Invoke or Timer with Coroutine

If timer goes to 5 seconds, enable the hint in a UI text object.

\$\endgroup\$
  • \$\begingroup\$ I cannot recommend a Invoke/coroutine solution - it seems like overkill. \$\endgroup\$ – wondra Jun 19 '16 at 12:02
  • \$\begingroup\$ appreciate it. was being beginner friendly. your's is efficient way. thanks. \$\endgroup\$ – Sourav Paul Jun 19 '16 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.