A
A
artemkaw2017-11-01 19:28:48
C++ / C#
artemkaw, 2017-11-01 19:28:48

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

1 answer(s)
S
Sergey Kormishin, 2017-11-01
@artemkaw

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 question

Ask a Question

731 491 924 answers to any question