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?