2016-08-04 24 views
0

我有一個腳本,5秒後重新生成玩家的生命:Unity2D:如何重新生成玩家的生命後,倒計時爲0

float counter; 

    void Update(){ 
      counter += Time.deltaTime; 
      if(counter > 30minutes){ 
       curHealth++; 
       counter = 0.0f; } 
       if(curHealth > 5) 
       curHealth = 5; 
       } 

這是連接到我的球員的健康腳本。當計時器爲0時,我想開始重新生成我的球員的健康狀況。但我不知道如何。 這是我的球員的健康腳本:

//Stats 
public int curHealth; 
public int maxHealth = 3; 
public Collider2D col; 
public PlayerHealth playerhealthRef; 
public TimeManager countDownTimer; 
float counter; 
public Animator anima; // drag the panel in here again 
private UI_ManagerScripts UIM; 
DateTime currentDate; 
DateTime oldDate; 

private GenerateEnemy generateEnemy = null; 


void Start() 
{ 
    curHealth = maxHealth; 
    currentDate = System.DateTime.Now; 
    if (countDownTimer != null) 
    { 
     // countDownTimer.gameObject.SetActive(false); 
     countDownTimer.Enable(false); 
    } 
    if (GameObject.Find("Spawner")) 
    { 
     generateEnemy = GameObject.Find("Spawner").GetComponent<GenerateEnemy>(); 
    } 
} 


void Update() 
{ 
    counter += Time.deltaTime; 
    if (curHealth > maxHealth) { 
     curHealth = maxHealth; 
    } 
    if ((curHealth <= 0) && (!countDownTimer.gameObject.activeSelf)) { 

     Die(); 
    } 
    if(counter > 5) 
    { 
     curHealth++; 
     counter = 0.0f; 
    } 
    if(curHealth > 3) 
     curHealth = 3; 
} 


void Awake() 
{ 
    UIM = GameObject.Find ("UIManager").GetComponent<UI_ManagerScripts>(); 
} 

void Die() { 
    UIM.EnableBoolAnimator(anima); 
    PlayerPrefs.SetInt("RemainingLives", curHealth); 
    PlayerPrefs.Save(); 

    if (generateEnemy != null) 
    { 
     generateEnemy.DestroyAllBlobs(); 
    } 
    if (countDownTimer != null) 
    { 
     countDownTimer.Enable(true); 
    } 
} 
public void Damage(int dmg) 
{ 
    curHealth -= dmg; 
} 
} 

這是我的定時器倒計時腳本:

public Text timer; 
int minutes = 1; 
int seconds = 0; 
float miliseconds = 0; 

[Range(1, 59)] 
public int defaultStartMinutes = 1; 
public bool allowTimerRestart = false; 
public bool useElapsedTime = true; 

private int savedSeconds = -1; 
private bool resetTimer = false; 

private DateTime centuryBegin = new DateTime(2001, 1, 1); 

private float tickPerSecond = 10000000.0f; 

public void Enable (bool enable) 
{ 
    gameObject.SetActive(enable); 
    ResetTime(); // force the timer to restart 
} 

void Awake() 
{ 
    minutes = defaultStartMinutes; 
    if (PlayerPrefs.HasKey("TimeOnExit")) 
    { 
     miliseconds = PlayerPrefs.GetFloat("TimeOnExit"); 
     savedSeconds = (int)miliseconds; 

     if (useElapsedTime && PlayerPrefs.HasKey("CurrentTime")) 
     { 
      int elapsedTicks = (int)(DateTime.Now.Ticks/tickPerSecond); 
      int ct = PlayerPrefs.GetInt("CurrentTime", elapsedTicks); 
      PlayerPrefs.DeleteKey("CurrentTime"); 
      elapsedTicks -= ct; 
      if (elapsedTicks < miliseconds) 
      { 
       miliseconds -= elapsedTicks; 
      } 
      else 
      { 
       miliseconds = 0; 
      } 
     } 

     minutes = (int)miliseconds/60; 
     miliseconds -= (minutes * 60); 

     seconds = (int)miliseconds; 
     miliseconds -= seconds; 

     PlayerPrefs.DeleteKey("TimeOnExit"); 

    } 
    savedSeconds = 0; 
} 

public void Update() 
{ 
    // count down in seconds 
    miliseconds += Time.deltaTime; 
    if (resetTimer) 
    { 
     ResetTime(); 
    } 
    if (miliseconds >= 1.0f) 
    { 
     miliseconds -= 1.0f; 
     if ((seconds > 0) || (minutes > 0)) 
     { 
      seconds--; 
      if (seconds < 0) 
      { 
       seconds = 59; 
       minutes--; 
      } 
     } 
     else 
     { 
      resetTimer = allowTimerRestart; 
     } 
    } 

    if (seconds != savedSeconds) 
    { 
     // Show current time 
     timer.text = string.Format("{0}:{1:D2}", minutes, seconds); 
     savedSeconds = seconds; 
    } 
} 

void ResetTime() 
{ 
    minutes = defaultStartMinutes; 
    seconds = 0; 
    savedSeconds = 0; 
    miliseconds = 1.0f - Time.deltaTime; 
    resetTimer = false; 
} 

private void OnApplicationQuit() 
{ 
    int numSeconds = ((minutes * 60) + seconds); 
    if (numSeconds > 0) 
    { 
     miliseconds += numSeconds; 
     PlayerPrefs.SetFloat("TimeOnExit", miliseconds); 

     if (useElapsedTime) 
     { 
      int elapsedTicks = (int)(DateTime.Now.Ticks/tickPerSecond); 
      PlayerPrefs.SetInt("CurrentTime", elapsedTicks); 
     } 
    } 
} 

}

謝謝:)

第二個編輯 我試着在我的更新文件中執行此操作:

if (countDownTimer = true) 
    { 
     if(counter > 5) 
     { 
      curHealth++; 
      counter = 0.0f; 
     } 
     if(curHealth > 3) 
      curHealth = 3; 
    } 

但我得到這個錯誤:錯誤CS0029:不能隱式轉換類型bool' to TimeManager'。

我只是不確定如何去做這件事。

+0

在你嘗試實現它的地方*正確*是你有問題嗎? –

+0

請看我上面編輯的問題 –

+0

'if(countDownTimer = true)'是一個錯誤。使用'if(countDownTimer)'爲true,'if(!countDownTimer)'爲false。或者你可以使用'if(countDownTimer == true)' – Wafer

回答

1

根據你的第二個編輯行1

if (countDownTimer = true) 

應該

if(countDownTimer) 

爲=運營商只給了一個值,併爲比較使用==,或者,如果變量是簡單的,如果(var)或者如果(!var)

0

你的大部分代碼是無用的。

您可以使用WaitForSeconds(它只是谷歌),或者你可以添加LeanTween到項目,並使用

LeanTweed.DeayedCall(5f,()=>{ /*code that will be runned after 5 sec will pass*/}); 

我更喜歡使用LeanTween。