OnTriggerEnter2D not working, making a turret

I have a very simple scene with 2 zombies and a turret:



The tower shoots arrows. It only instantiates them, further logic is handled by the script that is attached to an arrow. There is a script that makes it fly left with consistent speed and a script that damages a zombie.

The script to damage zombies looks like this:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArrowDamage : MonoBehaviour
{
    public float damage;

    void OnTriggerEnter2D(Collider2D col)
    {
        Debug.Log("Detected trigger");
        if (col.gameObject.tag == "Enemy")
        {
            Debug.Log("It's an enemy");
            col.gameObject.GetComponent<ZombieHP>().hp -= damage; //nothing fancy, when hp reaches 0 zombie gets destroyed
            gameObject.GetComponent<ArrowFly>().fly = false; //the whole script is just transform.Translate function that only works if fly is true
            gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero; //stop arrow from flying, just to make sure
            transform.parent = col.gameObject.transform; //attach arrow to a zombie object
        }
    }
}

A zombie has a collider attached to it with a rigidbody2D. Collider is not a trigger. Rigidbody2D is dynamic.

Arrow has no rigidbody2D, but has a collider set to be a trigger.

The problem is that OnTriggerEnter2D is not firing. It’s not even executing the first Debug.Log. But if I use OnCollisionEnter2D and put a rigidbody on an arrow that works.


I don’t want to use rigidbody2D on my arrow because it makes zombies slide backwards or loose speed after arrow becomes attached to them. Even if I can fix this it still creates bugs as arrows collide with each other after being attached to a zombie. And I don’t feel like having two rigidbodies2D on the same object is a good idea.

I’ve been searching for a good solution to implement this mechanic for 2 days now and it really makes me feel discouraged to continue developing. Am I that stupid that I miss something small and important?

You can use a rigidbody2D on arrows and then make the arrows kinematic (its an option on the rigidbody component). This will behave all the ways you want and will not push your zombies back at all. it also automatically removes gravity (which Im assuming you want).


Another option would be to leave the rigidbody dynamic but set the mass to a tiny amount (0.000001) and disable gravity. That way even though they do collide with the zombies, the arrows’ mass is so low that it will not affect the zombies in any meaningful way.


I personally use the OnCollsionEnter2D over the OnTriggerEnter2D because I find it easier to do what I want and more intuitive. but thats just personal preference and there is definitely a place and time to use OnTriggerEnter2D.


Cheers