- Home /
timed out load level
Hi I want to have a timed out load level so that if nothing is happening, ie no button has been pressed for 15 seconds then the game automatically loads the next level, so that if the player is not paying attention, the game automatically goes through all the levels. I assume that i need a boolean function ( have any buttons been pressed ?) , but am not sure how to write that..
Answer by Noah-1 · Jan 18, 2011 at 07:22 PM
Hello, you can use a simple timer like this, you can set it to 15 seconds. Check it out.
private var startTime; private var restSeconds : int; private var roundedRestSeconds : int; private var displaySeconds : int; private var displayMinutes : int;
var countDownSeconds : int;
function Awake() { startTime = Time.time; }
function OnGUI () {
//make sure that your time is based on when this script was first called //instead of when your game started
var guiTime = Time.time; //startTime;
restSeconds = countDownSeconds - (guiTime);
//Here you can diplay messages or change the prints for applications.LoadLevel ("YOUR LEVEL")
if (restSeconds == 60) { print ("Time is running"); } if (restSeconds == 0) {
print ("Time is over")
//do stuff here
}
//display the timer roundedRestSeconds = Mathf.CeilToInt(restSeconds); displaySeconds = roundedRestSeconds % 60; displayMinutes = roundedRestSeconds / 60;
text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); GUI.Label (Rect (220, 25, 100, 30), text);
}
Thanks but it isnt really what I was looking for. I was was looking for something that deter$$anonymous$$es that no activity has taken place, and then acts accordingly
Answer by Bunny83 · Jan 27, 2011 at 06:07 PM
After i looked into your other questions i guess you prefer JS (or Unityscript). Just setup a simple "timer" and you have to reset it when the user do "something". The "something" could be Input.anyKey and / or Input.mousePosition and / or Input.touchCount > 0 and / or accelerationEventCount > 0 if you work on iPhone (i guess you do).
var Timeout : float = 15; var currentTimeout : float; var OldMousePos : Vector3;
function Start() { ResetTimeout(); }
function ResetTimeout() { currentTimeout = Time.time + Timeout; }
function Update() { if (Input.anyKey || (Input.accelerationEventCount > 0) || (Input.touchCount > 0) || (Input.mousePosition != OldMousePos)) { ResetTimeout(); } if (Time.time > currentTimeout){ ResetTimeout(); // TODO: Load your level here } OldMousePos = Input.mousePosition; }
thanks so much Bunny ! it works perfect now ( i just had to make some adjustments now of course )
Your answer
Follow this Question
Related Questions
Wait n-seconds before loading level 1 Answer
FPSPlayer script from FPS tutorial won't work... 0 Answers
How Do I Exit Level? 1 Answer
scene loader issue 0 Answers
Saving a variable through loads 1 Answer