I fixed it myself
Problem with hit boxes in 2D
EDIT: I fixed it! Turns out the box collider was not active for long enough to register anything. I didn't know that was a thing. I added an attack duration variable to say that the box collider has to be active for the duration and it worked!
So I have an enemy that attacks a structure using a simple BoxCollider2D on a cooldown. The cooldown works just fine and the box collider is for sure being activated and deactivated accordingly, but neither OnTriggerEnter2D OR OnTriggerExit2D are ever called
using UnityEngine;
using System.Collections;
public class AttackBox : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Collision detected");
if (other.CompareTag("Fortress"))
{
ScoreManager sm = GameObject.FindObjectOfType<ScoreManager>();
sm.fortDamage();
}
}
void OnTriggerExit2D(Collider2D other)
{
Debug.Log("Collision detected");
if (other.CompareTag("Fortress"))
{
ScoreManager sm = GameObject.FindObjectOfType<ScoreManager>();
sm.fortDamage();
}
}
}
I've tried everything I can think of. The structure is not a trigger and has a RigidBody2D on it. The hit box is a child of the enemy and is a trigger. NOTE: I did make a blank scene just to try it with barebones objects and scripts and it DID work, but I can't figure out why!
Follow this Question
Related Questions
Why nothing happens if a Box Collider enters a Trigger? 2 Answers
OnTriggerEnter2D getting called earlier than it's supposed to 0 Answers
How to disable a boxcollider2d temporary? 1 Answer
GameObject keeps colliding for sometime after being destroyed 1 Answer
Instantiate object in 2D game? 3 Answers