- Home /
How to use collision.name in custom function
So at the moment I'm suck trying to make a enemy stop after being hit. The problem is that I can't use the collision.name because it doesn't exist in the context but I also can't put Collider2D as a parameter on the end of the function because I need the int damage there. So how would I be able to do this?
public void TakeDamage(int damage)
{
anim.SetTrigger("hurt");
if(collision.name == Hawk)
{
GetComponent<StopHit>().enabled = true;
}
health -= damage;
if (health <= 0)
{
Destroy(gameObject);
}
}
Answer by axlxi1 · Oct 11, 2020 at 12:19 PM
one possible solution in here would be to overload you're TakeDamage function if you still want to have TakeDamage(int damage) and do not change it. you can overload this like this :
public void TakeDamage(int damage, Collider2D collision)
{
anim.SetTrigger("hurt");
if(collision.name == Hawk)
{
GetComponent<StopHit>().enabled = true;
}
health -= damage;
if (health <= 0)
{
Destroy(gameObject);
}
}
Hey, thank you so much, it worked all good. I have one more question though is it possible to call a IEnumerable function, maybe something to do with coroutines ? Sadly it isn't working. @axlxi1
public void TakeDamage(int damage, Collider2D collision)
{
anim.SetTrigger("hurt");
if(collision.name == "Hawk")
{
StopWhenHit();
}
health -= damage;
if (health <= 0)
{
Destroy(gameObject);
}
}
public IEnumerable StopWhenHit()
{
stopwhenhit.GetComponent<AIDestinationSetter>().enabled = false;
yield return new WaitForSeconds(5f);
stopwhenhit.GetComponent<AIDestinationSetter>().enabled = true;
}
glad it works. you can use IEnumerator that's probably gonna be good here public void TakeDamage(int damage, Collider2D collision) { anim.SetTrigger("hurt");
if(collision.name == "Hawk")
{
StartCoroutine(StopWhenHit());
}
health -= damage;
if (health <= 0)
{
Destroy(gameObject);
}
}
public IEnumerator StopWhenHit()
{
stopwhenhit.GetComponent<AIDestinationSetter>().enabled = false;
yield return new WaitForSeconds(5f);
stopwhenhit.GetComponent<AIDestinationSetter>().enabled = true;
}
Your answer
Follow this Question
Related Questions
Enemy stops patrolling when player comes close 2 Answers
How to make arch knockback 3 Answers
Help with bombs destroying colliders in different position from gameobject 0 Answers
How to check if a enemy is moving up or down? 1 Answer
Fist Punch Collision 0 Answers