如何创建目标系统

问题描述 投票:-2回答:1

我是Unity和C#的新手。我正在尝试设计一个目标系统。我的游戏有3个目标,完成所有房屋,杀死所有巨龙并拯救所有人。我只想在完成所有目标后加载Win Scene。我有6个布尔值存储数据。 3个是公开的,允许我选择必须在检查员中完成的目标。 3和private检测目标何时完成。如何创建目标系统。以下是我的脚本,其中包含信息。

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

public class SceneController : MonoBehaviour
{
    private GameObject[] StartHouseCount;
    private GameObject[] StartDragonCount;
    private GameObject[] LiveDragonCount;
    private GameObject[] FinishedHouseCount;

    public int NumOfHouses;
    public int NumOfFinishedHouse;
    public int NumOfDragons;
    public int LiveNumOfDragons;
    public GameObject[] Players;
    public GameObject CurrentPlayer;

    [Header("Player")]
    public float RefuelRate;
    public float RepairRate;

    public GameObject canvas;
    public GameObject SettingsMenu;
    public bool GameIsPaused = false;

    private GameObject MainPlayer;

    [Header("Management")]
    public bool Win = false;
    public int Level;
    private GameObject GameManagement;

    [Header("Objectives")]
    public bool FinishAllHouses = true;
    public bool KillAllDragons = false;
    public bool RescueAllPeople = false;
    private bool FAHDone = false;
    private bool KALDone = false;
    private bool RAPDone = false;

    // Start is called before the first frame update
    void Awake()
    {
        CurrentPlayer = Players[0];
        StartHouseCount = GameObject.FindGameObjectsWithTag("House");
        StartDragonCount = GameObject.FindGameObjectsWithTag("Dragon");
        NumOfHouses = StartHouseCount.Length;
        NumOfDragons = StartDragonCount.Length;
        MainPlayer = Players[0];
        GameManagement = GameObject.FindGameObjectWithTag("GameManagement");
    }

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

        GameIsPaused = canvas.GetComponent<PauseMenu>().GameIsPaused;

        LiveDragonCount = GameObject.FindGameObjectsWithTag("Dragon");
        LiveNumOfDragons = LiveDragonCount.Length;

        FinishedHouseCount = GameObject.FindGameObjectsWithTag("ThankYou");
        NumOfFinishedHouse = FinishedHouseCount.Length;

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            CurrentPlayer = Players[0];
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            CurrentPlayer = Players[1];
        }
        if (NumOfFinishedHouse == NumOfHouses)
        {
            FAHDone = true;
        }
        if (LiveNumOfDragons == 0)
        {
            KALDone = true;
        }
        if (GameObject.FindGameObjectsWithTag("People").Length == 0)
        {
            RAPDone = true;
        }

        if (NumOfFinishedHouse == NumOfHouses && MainPlayer.GetComponent<TransportCopter>().CurrentPeople == 0)
        {
            Win = true;
        }

        if (MainPlayer == null)
        {
            Win = false;
            SceneManager.LoadScene("LoseScene");
        }
        if (MainPlayer.GetComponent<BasicHelicopterController>().CurrentFuel <= 0 || MainPlayer.GetComponent<BasicHelicopterController>().CurrentHealth <= 0)
        {
            Win = false;
            SceneManager.LoadScene("LoseScene");
        }
        if (Win == true)
        {
            GameManagement.GetComponent<GameManagement>().LevelComplete[Level] = true;
            SceneManager.LoadSceneAsync("WinScene");
        }

    }
}
c# unity3d
1个回答
0
投票

我找到了解决此问题的简单方法。当所有FAHDone,KALDone和RAPDone为真时,我宣布获胜。在脚本中,当它们实际完成或相应的public bool设置为false时,我将它们设置为true。仅此而已。

© www.soinside.com 2019 - 2024. All rights reserved.