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