簡體   English   中英

如何限制我的玩家在 Unity 中重生的次數?

[英]How do I limit the amount of times my player can respawn in Unity?

我正在嘗試將我的玩家在游戲結束屏幕顯示之前可以重生的次數限制為 3 或 5 次。 我該怎么做呢? 我到底如何在最后編碼/添加屏幕? 我應該只在屏幕上調用(從另一個場景)嗎? 我是 Unity 的新手,如果這沒有意義,我很抱歉。 誰能幫我?

這是您將如何解決問題的邏輯。 有一個計數器,默認值為 5。每次玩家重生時,你都會從計數器中減少,所以你知道他們死了多少次。 然后你有一個檢查,如果他們還沒有死 5 次,重生,但如果他們死了 5 次,游戲就結束了。 對於游戲結束菜單,不要過渡到新場景。 在同一場景中有一個禁用游戲object,並在游戲結束時激活它。

public int respawns = 5;  // create a variable for how many times the player can respawn before displaying the gameover screen
public GameObject gameOverMenu; // this is the menu you want to activate in unity when they can no longer respawn

private void Start ()
{
    gameOverMenu.SetActive (false); // make sure that the game over menu is off by default

}

private void Update()
{
    if (YOUR PLAYER DIES)
    {
        if (respawns > 0) // if they haven't respawned 5 times yet
        {
            //respawn the player
            respawns--; // remove 1 from the amount of time they can respawn
        }
        else 
        {
            gameOverMenu.SetActive (true); // enable the game over menu
        }
    }
 }

邏輯就在那里,你必須將它插入到你的腳本中。 如果您想要更直接的答案,請發布您當前的腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM