- Home /
Looking for a script to call function if there is no user input for set amount of time.
Hi,
I'm new to unity and fairly new to programming in general.
My current problem is that I have a program which allows the user to toggle between different cameras. The user can control the cameras with the A,S,D,W keys plus the mouse itself to look around. What I'm looking for is a script that will keep checking whether there has been any user input, from the keys or mouse, and if there hasn't been, for example, 90 seconds call a function to revert back to the main menu.
Any advice on how best to accomplish this is much appreciated.
Answer by duck · May 03, 2010 at 07:09 PM
Something like this should do it. I'm using the GetAxis functions because - by default - WASD and the Arrow keys both map to the horizontal and vertical axes.
private var lastActivity = 0; var idleThreshold = 90;
function Start() { lastActivity = Time.time; }
function Update() {
var h = Input.GetAxis("Horizontal");
var v = Input.GetAxis("Vertical");
var x = Input.GetAxis ("Mouse X");
var y = Input.GetAxis ("Mouse Y");
if ( h != 0 || v != 0 || x != 0 || y != 0 ) {
lastActivity = Time.time;
}
if (Time.time > lastActivity + idleThreshold) {
EscapeToMenu();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612210317im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Stop Watch start when told 2 Answers
Instanced Prefabs won't face camera. 2 Answers
Inspection camera script problem 2 Answers
Aligning Camera to Object 2 Answers