我有一个脚本可以在5秒后重现玩家的健康状况:
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 :(得分:1)
根据你的第二个编辑专栏1
if (countDownTimer = true)
应该是
if(countDownTimer)
as =运算符只是给出一个值,为了比较你使用==,或者,如果变量是bool,只需要(var)或if(!var)
答案 1 :(得分:0)
你的大部分代码都没用。
您可以使用WaitForSeconds(只是谷歌)或者您可以将LeanTween添加到您的项目并使用
LeanTweed.DeayedCall(5f, ()=>{ /*code that will be runned after 5 sec will pass*/});
我更喜欢使用LeanTween。