x


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.

more ▼

asked May 03, 2010 at 06:47 PM

Rick 1 gravatar image

Rick 1
21 6 4 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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();
    }

}
more ▼

answered May 03, 2010 at 07:09 PM

duck gravatar image

duck ♦♦
47.2k 129 188 457

Works an absolute treat! Thanks a lot, Duck!

May 03, 2010 at 07:23 PM Rick 1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4748
x4712
x1512
x858
x46

asked: May 03, 2010 at 06:47 PM

Seen: 1568 times

Last Updated: May 03, 2010 at 06:47 PM