- Home /
Question by
LunatiXx · Jan 19, 2019 at 11:36 PM ·
enemydamageinvulnerability
Temporary invulnerability in a platformer
Hello I'm working on a 2d platformer and I'm trying to give my player invulnerability for 3 seconds after getting hit. but I just cant seem to get it to work. any ideas how to fix this script?
public class Invulnerability : MonoBehaviour
{
Renderer rend;
Color c;
void start ()
{
rend = GetComponent<Renderer>();
c = rend.material.color;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == ("Enemy") && GameControlScript.health > 0)
StartCoroutine ("GetInvulnerable");
}
IEnumerator GetInvulnerable()
{
Physics2D.IgnoreLayerCollision (8, 10, true);
c.a = 0.5f;
rend.material.color = c;
yield return new WaitForSeconds (3f);
Physics2D.IgnoreLayerCollision (8, 10, false);
c.a = 1f;
rend.material.color = c;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612190057im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Temporary invulnerability in a platformer 1 Answer
Temporary invulnerability in a platformer 0 Answers
Damage on prefab collision? 0 Answers
Enemy Damage to Player 0 Answers