Question by
aarninja14 · Sep 08, 2020 at 07:01 PM ·
damagenoobhealth
How do I do a Immunity time after being hited?
I want to have an immunity time after being hit so that I don't lose all my health at once I've tried many forms but I can't find a solution here is my code
void Update()
{
if (isImmune = false && objeto1.IsTouching(objeto2))
{
Takedamage(1);
isImmune = true;
if (isImmune == true)
{
immunityTime = immunityTime + Time.deltaTime;
if (immunityTime >= immunityDuration)
{
isImmune = false;
}
}
}
}
void Takedamage(int damage)
{
currentHealth -= damage;
healthbar.SetMaxHealth(currentHealth);
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
Object.Destroy(gameObject);
}
Could you please help me I really don't know how to continue
Comment
Best Answer
Answer by Hellium · Sep 09, 2020 at 07:42 AM
void Update()
{
if (isImmune == false && objeto1.IsTouching(objeto2))
{
Takedamage(1);
isImmune = true;
immunityTime = 0;
}
else if (isImmune == true)
{
immunityTime = immunityTime + Time.deltaTime;
if (immunityTime >= immunityDuration)
{
isImmune = false;
}
}
}
Answer by streeetwalker · Sep 09, 2020 at 07:22 AM
create a coroutine that contains a while loop and checks your a counter against your immune time.
For example:
// where you get hit:
StartCoroutine( DoImmunity( immuneTime ) );
//then the coroutine
IEnumerator DoImmunity( float immuneTime ) {
isImmune = true;
float timer = 0;
while( timer < immuneTime ){
yield return null; // wait for the next Update frame
timer += Time.deltaTime;
}
isImmune = false;
}
you should be able to adapt that to fit your code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613011500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Apply Damage Universally 0 Answers
Enemy Can't Take Damage!!! 0 Answers
[Solved] I can't call Method from Referenced Script 1 Answer
Inconsistent 2d collider hit registration 3 Answers
Kill Count and Health Bar Scripts 0 Answers