- Home /
Question by
LunatiXx · Jan 26, 2019 at 03:51 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
If you dont want to interact with anything along 3 secs. just disable your players collider component when u hit and invoke your predefined enabling method ins$$anonymous$$d of StartCoroutine ("GetInvulnerable");
if (col.gameObject.tag == ("Enemy") && GameControlScript.health > 0){
//disable collider and do anytihng you want to do before invulnerability
Invoke("Enable", 3);
}
void Enable(){
//do your stuff and enable collider again
}
Your answer
Follow this Question
Related Questions
Temporary invulnerability in a platformer 1 Answer
Temporary invulnerability in a platformer 0 Answers
Object Not Recieving Damage 3 Answers
Staged bullet damage? 1 Answer
My Enemy Damage Script not working 1 Answer