Detect when no input is recieved

Hello there, I am new to Unity and trying to make a game where an action is triggered if no input is received from the player (such as if the player doesn’t make an input within 5 seconds, they die). If anybody knows how to do this, please advise.

You just need a simple timer in your player’s update method. The timer would get reset whenever the player makes an input. It would be something like this:

private float inputTimer;

void Start()
{
    inputTimer = 0;
}

void Update()
{
    inputTimer += Time.deltaTime;

    //Then you do your usual input checks
    if(Input.GetKeyDown(KeyCode.Space))
    {
        //Reset the timer
        inputTimer = 0;
        //Do player action
    }
    //5 seconds or anything you want, I would turn that into a public field so you can change it from the inspector
    if(inputTimer >= 5f)
    {
        inputTimer = 0;
        //Kill player or whatever
    }
}

And that’s it, if you use more keys for your inputs or mouse clicks or whatever just make sure you reset the timer. If you are using axis inputs or mouse movement just reset the value when the input is different from zero.

using System;
using UnityEngine;

public class CheckUserInput : MonoBehaviour
{
    public float HintDuration = 5f;
    public Action UserAction;

    private float inputTimer = 0;

    void Update()
    {
        inputTimer += Time.deltaTime;

        bool isMouseMove = Input.GetAxis("Mouse X") !=0 || Input.GetAxis("Mouse Y")!=0;

        if (Input.anyKey|| isMouseMove)
        {
            inputTimer = 0;
        }

        if (inputTimer >= HintDuration)
        {
            inputTimer = 0;
            UserAction?.Invoke();
        }
    }
}

/* //Use case
 
 public class Demo : MonoBehaviour {

    public CheckUserInput checkUserInput;

	void Start ()
    {
        if (checkUserInput!=null)
        {
            checkUserInput.UserAction = UserInputHint;
        }
    }


    void UserInputHint()
    {
        Debug.Log("hello,world!");
    }
}
 
     
*/