- Home /
deciding between different Objects hitting the same collision
I am searching for an easy Method to decide between different Objects hitting the same collision. So, I have a 2D shooting game, where your purpose is shooting things, that fly towards you. You have 2 different arrow and (right now) 2 enemy types. The plan is that arrow A kills enemy A and itself, while arrow B just kills itself on contact with enemy A. It would be the other way around for enemy B.
Essentially, the script would be:
private void OnCollisionEnter(Collision collision)
{
if (gameObject.CompareTag("EnemyA"))
{
if (collision.gameObject.CompareTag("ArrowA"))
{
// If arrow had tag 'ArrowA':
// - Kill enemy A
// - Destroy arrow
Die();
Destroy(collision.gameObject);
}
else if (collision.gameObject.CompareTag("ArrowB"))
{
// If arrow had tag 'ArrowA':
// - Destroy arrow
Destroy(collision.gameObject);
}
}
else if (gameObject.CompareTag("EnemyB"))
{
if (collision.gameObject.CompareTag("ArrowB"))
{
// If arrow had tag 'ArrowB':
// - Kill enemy B
// - Destroy arrow
Die();
Destroy(collision.gameObject);
}
else if (collision.gameObject.CompareTag("ArrowA"))
{
// If arrow had tag 'ArrowA':
// - Destroy arrow
Destroy(collision.gameObject);
}
}
}
private void Die()
{
// Destroy enemy or die idk
}
Remember to create and assign the correct tags to the correct objects. @Fusionsmask
Answer by xxmariofer · Dec 22, 2020 at 08:36 AM
add tags and compare each tag, or play with the collisions matrix if you want arrow A to ignore enemy B
The objects have tags but I struggle with comparing them. Could you help me with that?
Your answer
![](https://koobas.hobune.stream/wayback/20220613022009im_/https://answers.unity.com/themes/thub/images/avi.jpg)