- Home /
Only use specific collider
Hello!
So what i want to do is make an enemy do stuff only when a 2D collider attached to a bullet gameobject touches the enemy. Now the enemy will react to every collider that touches him. How would i go about to fix this?
void OnTriggerEnter2D (Collider2D other) {
Debug.Log ("Zombie Hit");
}
Thank you!
/Taegos
Answer by Positive7 · Jul 28, 2015 at 10:56 PM
Set tag on enemy to " Enemy "
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.CompareTag("Enemy")) {
Debug.Log ("Zombie Hit");
}
}
Thank you, but if 2 enemies collide with each other i get this message. I only want "Zombie Hit" if it is the bullet that hits them. This is a step on the way though, thank you :)
Lol i just changed the "Enemy" - tag to "Projectile", you put me on the right track though, thank you :)
Ohh, so the script is attached to the Enemy? in that case tag bullet as " Bullet " and if (other.gameObject.CompareTag("Bullet")) or by its name if (other.gameObject.name == "bullet")
Answer by bchen5803 · Jul 29, 2015 at 12:43 AM
I'm new to unity as well so I'm not so sure but I believe you can use the Tag function. Attach a tag called bullet to the bullet and then do something like this.
void OnTriggerEnter2D (Collider2D other) {
if(other.CompareTag("Bullet"))
Debug.Log ("Zombie Hit");
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Read txt file into a game 1 Answer
Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer
Dying Script error 2 Answers