Unity-Breakout游戏与健康栏

时间:2017-10-20 18:51:33

标签: c# unity3d

我正在进行突破游戏,并且我想添加一个健康栏,当球触及某个标签时会减少标签" hazard"。我有一个游戏管理器脚本和一个Pickup交互脚本,但是按照我设置的方式,我有点混淆了如何从我的GM脚本触发takedamage到我的拾取脚本,考虑到我放了我的playerhealth元素进入我的GM脚本,所以我可以将它附加到空的gameobject调用游戏管理器,因为实际的玩家不在层次结构中,而是在运行时实例化。我希望我不必为此目的重做整件事。如果有人可以帮我解决这个问题,我会很感激。

这是我的GM脚本:

public class GM : MonoBehaviour
{

    public int lives = 3;
    public int bricks = 20;
    public float resetDelay = 1f;
    public Text livesText;

    public GameObject gameOver;
    private GameObject clonePaddle;
    public GameObject youWon;
    public GameObject bricksPrefab;
    public GameObject paddle;
    public GameObject deathParticles;
    public static GM instance = null;

    public int startingHealth = 100;
    public int currentHealth;
    public Slider healthSlider;

    bool isDead;
    bool damaged;



    void Awake()
    {
        currentHealth = startingHealth;
        TakeDamage(10);

        if (instance == null)
            instance = this;
        else if (instance != this)
            Destroy(gameObject);

        Setup();

    }

    public void TakeDamage(int amount)
    {
        damaged = true;

        currentHealth -= amount;
        healthSlider.value = currentHealth;

        if (currentHealth <= 0)
        {
            LoseLife();
        }

    }

    public void Setup()
    {
        clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
        Instantiate(bricksPrefab, transform.position, Quaternion.identity);
    }


    void CheckGameOver()
    {
        if (bricks < 1)
        {
            youWon.SetActive(true);
            Time.timeScale = .25f;
            Invoke("Reset", resetDelay);
        }

        if (lives < 1)
        {
            gameOver.SetActive(true);
            Time.timeScale = .25f;
            Invoke("Reset", resetDelay);
        }

    }

    void Reset()
    {
        Time.timeScale = 1f;
        Application.LoadLevel(Application.loadedLevel);
    }

    public void LoseLife()
    {
        lives--;
        livesText.text = "Lives: " + lives;
        Instantiate(deathParticles, clonePaddle.transform.position, Quaternion.identity);
        Destroy(clonePaddle);
        Invoke("SetupPaddle", resetDelay);
        CheckGameOver();
    }

    void SetupPaddle()
    {
        clonePaddle = Instantiate(paddle, transform.position, Quaternion.identity) as GameObject;
    }

    public void DestroyBrick()
    {
        bricks--;
        CheckGameOver();
    }
}

这是我的皮卡脚本:

public class Pickups : MonoBehaviour {

    public float PaddleSpeedValue = 0.5f;

    private bool isActive = false;

    public float thrust=20f;

    public Rigidbody rb;

    GameObject player;



    private void OnTriggerEnter(Collider other)
    {
         if (other.tag == "Hazard")
        {
            isActive = true;
            Destroy(other.gameObject);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

Pickups类根本不应该拥有或存储对玩家的引用(尽管从你的问题中不清楚这是一个附加到播放器的脚本还是附加到其他内容的脚本)。你的游戏经理课应该。毕竟,是负责管理游戏的类,其中包括玩家。从那里,您可以使用GetComponent<?>()访问附加到玩家GameObject的任何脚本。

然后,当GM包含对自身的公共静态引用时,任何其他类都可以根据需要通过GM.instance.player获得对玩家的引用,即使玩家被创建和销毁多次( GM应始终引用当前的!)

据推测,clonePaddle字段是玩家(游戏对象),您只需将其公开。