游戏:3D打地鼠游戏

235 阅读1分钟

学习目标:实现打地鼠核心玩法

实现思路:

在实现“地鼠在地图上指定的9个位置随机生成”部分,了解下实现原理:

(1) 定义一个生成函数;

(2) 定义场景中的9个位置点;

(3) 在9个位置点分别实例化;

(4) 在Start函数中进行调用生成函数(测试在Unity中是否可以实现);

(5) 在生成函数中编写随机生成代码;

(6) 继续在生成函数中编写循环代码;

(7) 在Start函数中编写代码(确定每隔多久生成几个):InvckeRepeating(“Creat,0,1”);

Creat:表示运行该函数;

0:表示隔0秒生成;

1:表示每次生成1个。

在最后对地鼠洞口与地鼠位置进行对齐时,需要适当对摄像机属性进行调整。如果在Game游戏窗口看不到物体画面时,可以适当调整物体位置属性。

游戏画面

项目结构目录

部分核心代码

public class GameManager : MonoBehaviour
{
    public static GameState gameState = GameState.start;
    public Text scoreText;
    public static int score;
    public Slider timeSlider;
    public static float playTime = 30;
    private float timeNum = playTime;
    public int dishuIndex = -1;

    public GameObject btnStart;
    public GameObject endWin;
    public GameObject[] didong;

    private float stopTime = 0.7f;

    // Start is called before the first frame update
    void Start()
    {
        GameManager.gameState = GameState.start;
        GameManager.playTime = 30;
        timeNum = GameManager.playTime;
    }

    // Update is called once per frame
    void Update()
    {

        if (GameManager.gameState == GameState.start ||
        GameManager.gameState == GameState.over)
        {
            return;
        }

        // 倒计时游戏结束
        GameManager.playTime -= Time.deltaTime;
        timeSlider.value = GameManager.playTime / timeNum;
        print("gameTime == " + timeSlider.value);
        if (timeSlider.value <= 0)
        {
            GameManager.gameState = GameState.over;
            endWin.SetActive(true);
            endWin.GetComponent<EndWin>().Init("游戏结束");
        }

        // 点击鼠标打老鼠
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject.tag == "mouse")
                {
                    print("打住小老鼠");
                    // GameManager.gameState = GameState.jizhong;
                    hit.collider.gameObject.GetComponent<DiShu>().jizhongs();
                }
            }
        }

        // 出现老鼠
        if (GameManager.gameState == GameState.runying ||
         GameManager.gameState == GameState.jizhong ||
         GameManager.gameState == GameState.playing)
        {
            stopTime -= Time.deltaTime;
            if (stopTime <= 0)
            {
                ShowMouse();
                stopTime = 0.7f;
            }
        }

        scoreText.text = score.ToString();

        Debug.DrawRay(Camera.main.ScreenPointToRay(Input.mousePosition).origin, Camera.main.ScreenPointToRay(Input.mousePosition).direction * 100, Color.blue);
    }

    // 出现老鼠
    private void ShowMouse()
    {
        int ran = Random.Range(0, 9);
        // print("随机值" + ran);
        if (ran != dishuIndex)
        {
            dishuIndex = didong[ran].GetComponent<DiDong>().InitDiDong();
        }
    }

    // 开始按钮
    public void OnStartBtnClick()
    {
        btnStart.SetActive(false);
        GameManager.gameState = GameState.playing;
    }
}

下载链接:WhacMoleDemo: 打地鼠游戏demo