- Home /
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
{
private Transform Enemy;
Renderer rend;
Color c;
void start ()
{
Enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
rend = GetComponent<Renderer>();
c = rend.material.color;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == ("Enemy") && GameControlScript.health > 0)
Invoke ("GetInvulnerable", 0.1f);
}
IEnumerable 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;
}
}
I'm gonna try... edit: it now kinda works... but ins$$anonymous$$d of going back to normal it gives me invulnerability forever
Why are you using Invoke rather than StartCoroutine? it is not finishing your coroutine? It is the first time i see someone call a coroutine with invoke so not sure if that can give some errors.
Answer by ray2yar · Jan 19, 2019 at 11:02 PM
You need to use "StartCoroutine" instead of invoke. You CAN do this by invoking if you prefer but the structure would be different.
Follow this Question
Related Questions
Temporary invulnerability in a platformer 0 Answers
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