茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671260 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_00054.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

Game Play Maker : [轉貼]Unity 3d Countdown Timer

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Unity 3d Countdown Timer
Countdown Timer

Hey I need help

I would like a timer for my game but its like a game show timer so like say you have 3 mins to complete something then when 10 secs are left the timer comes up on the screen i would like something like that but i have no idea how to do it
guitimer

check my answer on this question : http://answers.unity3d.com/questions/279434/problem-restart-time-by-key.html
and this : http://answers.unity3d.com/questions/231521/how-to-display-data-in-an-array.html
all the information you need to do this are in those 2 answers =]
also : http://lmgtfy.com/?q=unity+countdown+timer
EDIT : here is what you're after ....
    #pragma strict

    var theTimer : float = 0.0;
    var theStartTime : float = 120.0;
    var showRemaining : boolean = false;

    function Start()
    {
    theTimer = theStartTime;
    }

    function Update()
    {
    theTimer -= Time.deltaTime;

    if (theTimer < 10)
    {
    Debug.Log("TEN SECONDS LEFT !");
    showRemaining = true;
    }

    if (theTimer <= 0)
    {
    Debug.Log("OUT OF TIME");
    theTimer = 0;
    }

    if ( Input.GetKeyUp(KeyCode.G) )
    {
    Debug.Log("Resetting");
    theTimer = theStartTime;
    showRemaining = false;
    }
    }

    function OnGUI()
    {
    var text : String = String.Format( "{0:00}:{1:00}", parseInt( theTimer / 60.0 ), parseInt( theTimer % 60.0 ) );

    if (showRemaining)
    {
    GUI.Label( Rect( 10, 10, Screen.width - 20, 30), text );
    }
    }


Thats not what im looking for i have a script for a menu to come up but i need a script so it say 10 seconds left then starts counting down
Jul 21, 2012 at 10:26 PM dalekandjedi
    var showRemaining : boolean = false;

    ... in update
    if (remainingTime < 10secs)
    {
    showRemaining = true;
    }

    ... OnGUI
    if (showRemaining)
    {
    GUI.Box( Rect(0,0,100,20), remainingTime.ToString() );
    }



原文出處: Countdown Timer - Unity Answers
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Unity C# countdown timer
C# countdown timer

I'm trying to create a c# countdown timer for my level which should also be displayed in the GUI. The player needs to destroy certain items to get extra time. If the player runs out of time it's game over. It the player destroyed all the items they are presented with the time left and then can play again to see if they can finish the level faster with more time remaining.
I'm not sure what the script is to initiate a timer and then count down with seconds. The initial time should obviously be a public parameter you can tweak from inside the editor.
Running out of time should run an if statement to initiate a game over screen for losing. Destroying all the objects should stop the timer and record the remaining time to display in the winning game over screen.
Destroying certain objects will add time to the timer in seconds or fractions of seconds.
So I know what I want to do, but the Syntax is giving me nightmares. :-O Any help will be appreciated.

You could create a script that keeps track of how much time is left.
That object's update loop can subtract Time.deltaTime from that value, and check if it's below zero:
var timeLeft = 30;
    function Update()
    {
    timeLeft -= Time.deltaTime;
    if ( timeLeft < 0 )
    {
    GameOver();
    }
    }

Other objects could access that value, to display it in the GUI or add to it as needed.

Translated to C# it would be
    float timeLeft = 30.0f;
    void Update()
    {
    timeLeft -= Time.deltaTime;
    if(timeLeft < 0)
    {
    GameOver();
    }
    }


Michael,
That's quite a request, but I like a challenge, so here we go.
Firstly, the structure that probably best suits this is: - Game Controller (in charge of game win/loss and messaging) - Timer (GuiText object that displays the time left on screen. It is VERY important that Timer is a child of the Game Controller gameobject) - Items (the onscreen, destructable items you mentioned. These must also be child objects of the Game Controller)
The Game Controller would look something like this:
    using UnityEngine;
    using System.Collections;
    public float timeIncrease = 2;
    public bool timeElapsed = false;
    public int items;
    void Start()
    {
    //Gather how many items are remaining
    GameObject[] items = GameObject.FindObjectsWithTag("items") as GameObject[];
    itemsRemaining = items.length;
    //The timer, as a child of this gameobject, receive this and start the countdown using the timeRemaining variable
    BroadcastMessage("Start Timer", timeRemaining);
    }
    void Update()
    {
    if (itemsRemaining == 0)
    {
    //You win!
    }
    if (timeElapsed)
    {
    //You lose!
    }
    }
    //If the game controller receives this signal from the timer, it will end the game
    void timeHasElapsed()
    {
    timeElapsed = true;
    }
    //If the Game Controller receives this signal from a destroyed item,
    // it sends a message to the time object to increase the time left
    void itemDestroyed()
    {
    increaseTime();
    }
    void increaseTime()
    {
    broadcastMessage("timeIncrease", timeIncrease)
    }
    //I've included this dead function because I can't test the code myself right now and I don't want to leave
    // you with errors. IT may or may not be needed, though.
    void timeIncrease
    {}

The purpose of the Game Controller would be to receive the happenings of both the items and timers and pass the appropriate responses back and forth, as well as handle the win/lose condition.
Then, you would have this script attached to your timer object:
    using UnityEngine;
    using System.Collections;
    public float timeRemaining = 60f;
    void Start()
    {
    InvokeRepeating("decreaseTimeRemaining", 1.0, 1.0)
    }
    void Update()
    {
    if (timeRemaining == 0)
    {
    sendMessageUpward("timeElapsed");
    }
    GuiText.text = timeRemaining + " Seconds remaining!";
    }
    void decreaseTimeRemaining()
    {
    timeRemaining --;
    }
    //may not be needed, left it in there
    void timeElapsed()
    {}

And, in the script for your onscreen items, you would include this:
    void OnDestroy()
    {
    SendMessageUpwards("itemDestroyed")
    }
    void itemDestroyed()
    {}

I have to apologise in advance, though, I have no ability to test this code right now and I had to crank it out quickly, so it will have errors, but remember, Game Controller is the parent to the timer and items, as they will have to send messages back and forth and, in this code, the game controller will handle that.


原文出處:C# countdown timer - Unity Answers
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Unity Count Down Timer
Count Down Timer.

Ok, what I'm trying to do is make a count down timer that will start, at maybe 2 minutes, at the start of the game, and when It reaches 0:00 then some text will appear on the screen. I know how to do the text and how to start it, but I don't know how to make the actual timer, any Ideas?
P.S. I did check out the other two questions about counters and timers, but i couldn't do anything with that :/

I got a simple version of a countdown timer here. I'm sure there are other and more elegant versions out there (e.g. see this forum thread), but they are also a bit lengthy. See for yourself.

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

    //display messages or whatever here -->do stuff based on your timer
    if (restSeconds == 60) {
    print ("One Minute Left");
    }
    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 (400, 25, 100, 30), text);

}


there are many ways to do this. for example another way would be
var showText=false;
function StopWatch (time:float) { yield WaitForSeconds (time); showText=true; } function OnGUI () { if (showText==true) { GUI.Label (Rect(100,100,300,150),"this text will be shown after the timer ends"): } }

so how did you sort the rounding problem?? nevermind i got it all you need to do is change some variables. if anyone needs this in c# here you go :) you will need to change public CountDownSeconds in the inspector to any value of your choice ie, 10:00, you would write 600 in the inspector panel.
using UnityEngine;
using System.Collections;

public class TimerScript : MonoBehaviour
{
private float startTime;
private float restSeconds;
private int roundedRestSeconds;
private float displaySeconds;
private float displayMinutes;
public int CountDownSeconds=120;
private float Timeleft;
string timetext;
// Use this for initialization void Start ()
{
 startTime=Time.deltaTime;
}
void OnGUI()
{
 Timeleft= Time.time-startTime;
 restSeconds = CountDownSeconds-(Timeleft);
 roundedRestSeconds=Mathf.CeilToInt(restSeconds);
 displaySeconds = roundedRestSeconds % 60;
 displayMinutes = (roundedRestSeconds / 60)%60;
 timetext = (displayMinutes.ToString()+":");
 if (displaySeconds > 9)
 {
  timetext = timetext + displaySeconds.ToString();
 }
 else
 {
  timetext = timetext + "0" + displaySeconds.ToString();
  }
  GUI.Label(new Rect(650.0f, 0.0f, 100.0f, 75.0f), timetext);
  }
 }
}


Here, very simple (I am using the code in a 3Dtext):
    public var time : float;
    function Update () {
    time -= Time.deltaTime;
    var minutes : int = time / 60;
    var seconds : int = time % 60;
    var fraction : int = (time * 100) % 100;
    if (time>0)
    //displaying in the 3Dtext
    GetComponent(TextMesh).text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
    }



原文出處:Count Down Timer. - Unity Answers
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[分享]Unity3d 倒數計時,冷日範本
說真格,後來搞懂以後,真的很簡單!
這裡提供冷日的寫法:
        private float time2CountDown = 60 * 5f;//看你要倒數 n 分鐘,就是 60 * n
        time2CountDown -= Time.deltaTime;
        int minutes = (int)(time2CountDown / 60);
        int seconds = (int)(time2CountDown % 60);
        clockCountDown.text = String.Format("{0:00}:{1:00} ", minutes, seconds);

把這一段寫進 update() 就可以囉!
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|