- Home /
Duplicate Question "and how"
CountDown Timer for Explosion
Hi guys.. I'm struggling with a countdown timer. I want to start a 10 second countdown when you enter a trigger to trigger an explosion. At the moment my timer starts when i press the play button but i want to start the timer when i enter the trigger. and it must stop at 0. here my code that i use now.
private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;
var countDownSeconds : int;
var showGUI : boolean = false;
function Start() {
startTime = Time.time;
}
function OnGUI () {
var guiTime = Time.time - startTime;
restSeconds = countDownSeconds - (guiTime);
if (restSeconds == 60) {
print ("One Minute Left");
}
if (restSeconds == 0) {
print ("Time is Over");
}
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
if(showGUI) {
GUI.Label (Rect (400, 25, 100, 30), text);
}
}
function OnTriggerEnter (player : Collider) {
showGUI = true;
yield WaitForSeconds(10.0);
if(player.gameObject.tag == "Player") {
GameObject.Find("cubetrigger").animation.Play("movetrigger");
}
}
function OnTriggerExit (other : Collider) {
Destroy(GameObject.Find("Sphere"));
}
Woah, this is a bit messy! It's ok, you have done well so far, but a couple of considerations, most importantly is OnGUI runs at least 2 times per frame/update, so putting calculations in OnGUI is not a good practice, very hard on the processor. The other is using yield, this should be in a coroutine or use a timer ins$$anonymous$$d. Finally I don't understand why so many people use Time.time in timers, just use a timer variable. I shall have a look at this and see what can be fixed/improved and added to answer your question =]
$$anonymous$$ann, just go to unityGE$$anonymous$$S.com
to learn how to use Invoke(). it's only one line of code.
this has been asked literally 1000s of times here.
Sorry, I know this question is closed, but I said I would help =]
So here is your code that I have edited with the considerations I gave in my comment (mainly not putting alot of calculations in OnGUI). I replaced your timer and put in 2 timers : one to measure gameTime and one to measure trigger time. You can see the part in the Update where if the triggerTimer reaches zero, BOO$$anonymous$$ !
Invoke could be very handy here, but without knowing what you had in $$anonymous$$d when writing this code, I have not used it, leaving the option to cancel the triggerTimer if required (by resetting isTriggered to false). Anyway, here 'tis :
#pragma strict
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var display$$anonymous$$inutes : int;
private var isTriggered : boolean = false;
public var countDownSeconds : int = 180;
private var timer : float = 0.0;
private var triggerTimer : float = 0.0;
public var triggerTimer$$anonymous$$ax : float = 10.0;
private var textTimer : String = "";
private var textTriggerTimer : String = "";
function Update()
{
timer += Time.deltaTime;
restSeconds = countDownSeconds - (timer);
if (restSeconds == 60) {
Debug.Log("One $$anonymous$$inute Left");
}
if (restSeconds == 0) {
Debug.Log("Time is Over");
}
roundedRestSeconds = $$anonymous$$athf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
display$$anonymous$$inutes = roundedRestSeconds / 60;
textTimer = String.Format ("{0:00}:{1:00}", display$$anonymous$$inutes, displaySeconds);
// - trigger timer -
if ( isTriggered )
{
triggerTimer -= Time.deltaTime;
textTriggerTimer = $$anonymous$$athf.CeilToInt( triggerTimer ).ToString();
if ( triggerTimer <= 0 )
{
// BOO$$anonymous$$ !
Debug.Log( "BOO$$anonymous$$ !" );
textTriggerTimer = "BOO$$anonymous$$ !";
GameObject.Find("cubetrigger").animation.Play("movetrigger");
}
}
}
function OnGUI()
{
if ( isTriggered )
{
GUI.Label( Rect( 25, 25, 100, 30 ), textTriggerTimer ); // trigger timer
GUI.Label( Rect( 400, 25, 100, 30 ), textTimer ); // game timer
}
}
function OnTriggerEnter( other : Collider )
{
if ( other.gameObject.tag == "Player" )
{
isTriggered = true;
triggerTimer = triggerTimer$$anonymous$$ax; // set timer to max for countdown to zero
}
}
function OnTriggerExit( other : Collider )
{
Destroy(GameObject.Find("Sphere"));
}
Follow Fatties advice, learn about Invoke (and InvokeRepeating, CancelInvoke), check out UnityGems (it has become a very resourceful 'site), and keep on coding !
Here are some links I strongly suggest to all new users :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
That is good to get started. Then start with a small tutorial, this is a simple 2D space shooter : http://www.unityjumpstart.com/ProofOfConcept_1/ : click on the videos part1.mp4 part2,3,4 =]
I just found another by our own Eric : http://wiki.unity3d.com/index.php?title=2DShooter : http://forum.unity3d.com/threads/7883-2D-shooter-tutorial
By then you should be getting the hang of things and starting to have ideas of your own. When you decide what kind of game you want to make, then look at each part you'll need. For example, if you want to make some terrain then walk around it with a character : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
Here's another on the same 'site but I havn't checked the difficulty : http://cgcookie.com/unity/2012/02/24/game-planning-lunar-lander-01/
Basically then just search for tutorials, there are many out there, either written or on youtube.
the Unity Wiki tutorials : http://wiki.unity3d.com/index.php/Tutorials
A big list of tutorials : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
A very helpful 'site, all in C# : http://unitygems.com/
Helpful page with information on using Built-In Arrays and Lists (you'll need this later!) : http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )
http://answers.unity3d.com/questions/148211/list-of-frequently-asked-beginners-questions.html
http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner
If you know what kind of game you want to make, edit your question and write there what you are thinking of, then there may be a tutorial out there for that!
But just be aware, you will do lots of typing, deleting, typing, bashing head on desk, being really happy when it works and you understand how and why. I hope this helps, Happy Coding =]
Follow this Question
Related Questions
countdown/countup timer 1 Answer
Countdown timer in minutes:seconds:milliseconds 2 Answers
Timer not work :( 0 Answers
Explosive like in COD 4? 1 Answer