hi i want to make player gets healt if its not hitted. I did that but there is one problem right after hit its health being full because of the health++ in update. How can i make this health++ slower? I tryed waitforseconds but it didn’t work.
if (Input.GetKey(KeyCode.E))
{
IsTakingDamage = true;
TakeHit(1f);
}else
{
IsTakingDamage = false;
}
if (IsTakingDamage == false)
{
while (currentHealth <= 100f)
{
currentHealth+= 0.001f*Time.deltaTime;
}
}
}
public void TakeHit(float damage)
{
currentHealth -= damage;
Healthbar.SetHealth(currentHealth, MaxHealth);
if (currentHealth <= 0)
{
Destroy(gameObject);
}
}
okey im idiot guys sory to bother you just did this code below and its worked very well thanks to you guys.
I realized after a long time that I could do this
if (IsTakingDamage == false)
{
if (currentHealth <= MaxHealth)
{
currentHealth += 3.0f * Time.deltaTime;
}
}
Just for future reference, when your code is running Unity is completely locked-up solid. Nothing else happens onscreen until you return from your code. And I mean NOTHING.
The only way to do a while loop AND have other stuff happen is if that while loop is: 1) in a coroutine, and 2) has a yield return statement inside it.
Otherwise 100% of that while loop will complete (if it is capable of completing) BEFORE the next frame renders. If the loop cannot complete, your program will lock up permanently and you will have to force-close the editor, losing any unsaved changes.