2018年5月23日 星期三

Week13_是沫不是沬

🌟期末專案_嗨!地鼠

🌠程式碼撰寫—除錯並撰寫第二關

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class NormalControl : MonoBehaviour {
    public static float gametimer;
    float roundTimer;
    int max;
    bool gameOver = false, round = false;
    public GameObject s2MouseOne, s2MouseTwo;
    public List<ControlMouse2> mouse = new List<ControlMouse2>();
    public List<Transform> hole = new List<Transform>();
 
    void Start()
    {
        gametimer = 45f;
        roundTimer = 0f;
        gameOver = false;
    }
    void Update()
    {
        if (!gameOver)
        {
            if (gametimer <= 0) gameOver = true;
            gametimer -= Time.deltaTime;
            roundTimer -= Time.deltaTime;
           
//Debug.Log(gametimer);

            if (!gameOver && roundTimer <= 0.2f)
            {
                round = true;
                roundTimer = 4f;
            }
            if (round)
            {
                max = Random.Range(3, 7);
                while (max > 0)
                {
                    max -= 1;
                    spawnS2Mouse();
                }
                round = false;
            }
        }
        if (gameOver)
        {
            SceneManager.LoadScene("Easy");
        }
    }
    void spawnS2Mouse()  //產生第二關的地鼠
    {
        int index = Random.Range(0, 12);
        while (mouse[index] != null) index = Random.Range(0, 12);
        //Debug.Log(index);
        if (index % 2 == 0)
        {

            GameObject s2One = Instantiate(s2MouseOne, new Vector3(hole[index].position.x,

                    hole[index].position.y - 5f, hole[index].position.z), hole[index].transform.rotation);
            mouse[index] = s2One.GetComponent<ControlMouse2>();
            Destroy(s2One, 3f);
        }
        else
        {

            GameObject s2Two = Instantiate(s2MouseTwo, new Vector3(hole[index].position.x,
                   hole[index].position.y - 30f, hole[index].position.z), hole[index].transform.rotation);
            mouse[index] = s2Two.GetComponent<ControlMouse2>();
            Destroy(s2Two, 3f);
        }
    }
}

執行結果如下


沒有留言:

張貼留言