Unity2D:如何重新生成玩家的生命后,倒计时为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

if (countDownTimer = true) 

应该

if(countDownTimer) 

为=运营商只给了一个值,并为比较使用==,或者,如果变量是简单的,如果(var)或者如果(!var)

你的大部分代码是无用的。

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

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

我更喜欢使用LeanTween。