Answer the question
In order to leave comments, you need to log in
Unity 3D C# - How to make a hero temporarily invulnerable when taking damage?
I am learning Unity 3D, writing in C#.
I'm experimenting with one game. Now I ran into a problem: how, when receiving damage, to make the main character gain invulnerability, for example, for 2 seconds (I have already implemented the taking of damage)?
Damage code:
Lives--;
if (lives == 0)
{
ReloadScene();
}
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(transform.up * 12.0F, ForceMode2D.Impulse);
Answer the question
In order to leave comments, you need to log in
You need to add one more lastTimeDamaged field and check it in your code like this:
if(Time.time > lastTimeDamaged + CooldownTime)
{
// your code
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question