Search code examples
unityscriptunity-game-engine

How to Display hints to user when he is inactive for more than 5 sec in unity?


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.


Solution

  • I dont agree with Joe Blow on this one that you need to call that EVERYWHERE in your code the user does something. What a user can do is press a key on the keyboard(also count on gamepads and controllers) or move the mouse(on mobile the mouse is simulated so that works too). So if you have a single class that looks something like this :

    using UnityEngine;
    public class TestInActive : MonoBehaviour {
        private Vector3 prevMousePosition = Vector3.zero;
        void ShowGameHintInvoke()
        {
            CancelInvoke();
            Invoke("GameHint", 5);
        }
        void GameHint()
        {
            Debug.Log("This is a Hint");
        }
        // Update is called once per frame
        void Update () {
            if (Input.anyKeyDown || Input.mousePosition != prevMousePosition)
                ShowGameHintInvoke();
            prevMousePosition = Input.mousePosition;
        }
    }
    

    It should work just fine. This calls ShowGameHintInvoke() once after the user has been inactive for 5 secs. Then it will not call it again until the user does something.