代码之家  ›  专栏  ›  技术社区  ›  Ninja2k

如何创建炸弹倒计时计时器?

  •  0
  • Ninja2k  · 技术社区  · 7 年前

    我正在制作一个使用炸弹和爆炸的统一游戏。我想限制玩家每3秒只能放置1颗炸弹,第一颗炸弹会爆炸,然后玩家可以放置另一颗,我为此创建的计时器似乎不起作用。

    当我玩游戏时,玩家可以放置炸弹,但在等待超过3秒后不能再放置炸弹。我的时间逻辑正确吗?

    public class CombatController : MonoBehaviour {
    
        //public GameObject enemy;
        [SerializeField]
        public GameObject Bomb;
        public GameObject Explosion;
        public int timer = 0;
    
    
        // Use this for initialization
        void Start () {
    
        }
    
        // Update is called once per frame
        void Update () {
    
            BombKeyPress ();   //Check if the user wants to plant the bomb.     
        }
    
    
        //Method BombKeyPress - This method will activate logic for our bombkeypress.
        public void BombKeyPress ()
        {
    
            if(Input.GetKeyUp(KeyCode.Space)) {
                BombDrop ();
    
            }
    
        }
    
        //Method BombDrop - This method will instantiate our bomb.
        private void BombDrop () 
        {
            //If the timer is not counting, allow the player to drop another.
            if (Bomb && timer <=0) {
                Instantiate (Bomb, this.gameObject.transform.position, Quaternion.identity); //Make New Bombs
                timer = 3; //Set our bomb timer to 3 seconds.
    
                //Countdown our bombtimer.
                for (int i = 3; i <= 0; i--) {
                    timer = i;
                    print(i);
                }
    
                //BombExplosion(); //Now blow up our bomb
    
            }   
    
        } 
    
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   bolkay Miamy    7 年前

    你可以利用 Time

    声明两个变量 BombRate 和 NextBombTime

    [SerializeField]
    private float BombRate=0.5f;// Tweak to value to your requirement.
    private float NextBombTime=0f;
    

    所以,在你的炸弹功能中,你可以有这样的东西:

    void Bomb (){
    if(Time.time>NextBombTime){
    NextBombTime=Time.time+BombRate;
    //Do your bomb stuff here
    }
    }
    

    如果愿意,也可以使用协同程序。

    我希望这有帮助。

        2
  •  1
  •   user6873176 user6873176    7 年前

    你需要使用一个协同程序来引爆炸弹,这样你就可以写下如下内容:

    //public GameObject enemy;
    [SerializeField]
    public GameObject Bomb;
    public GameObject Explosion;
    public float timer = 0;
    
    private float currentTime;
    private bool canBomb = true;
    
    
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
            BombKeyPress ();   //Check if the user wants to plant the bomb.   
    }
    IEnumerator BombExplosionTimer()
    {
        yield return new WaitForSeconds(timer);
        BombExplosion();
        canBomb = true;
    }
    
    public void BombExplosion()
    {
    
        //Put your Bomb explosion logic here
    }
    
    
    //Method BombKeyPress - This method will activate logic for our bombkeypress.
    public void BombKeyPress ()
    {
    
        if(Input.GetKeyUp(KeyCode.Space) && canBomb) {
            canBomb = false;
            BombDrop ();
            StartCoroutine(BombExplosionTimer());
        }
    }
    
    
    
    //Method BombDrop - This method will instantiate our bomb.
    public void  BombDrop () 
    {
        //If the timer is not counting, allow the player to drop another.
    
            Instantiate (Bomb, this.gameObject.transform.position, Quaternion.identity); //Make New Bombs
            timer = 3; //Set our bomb timer to 3 seconds.
    
    }