Using a colider to detect presence of a trigger (C#)

I’m going to refer to a colider not set as a trigger a “colider”, and a colider set as a trigger a “trigger”.

I have a shaped trigger attached to my player so that it can act as a sword swing area. when the enemy enters the trigger and you “swing your sword” I want the enemy to receive damage. unfortunately, with tags, the player cannot tell which enemy receives damage, and as far as I can tell, a colider cannot detect if it is being touched by a trigger.

Is it possible to to add another colider to my enemy as a trigger, and have it detect the players trigger? Is there a way for the specific enemy colider to tell when it’s in the player trigger?

It should be quite possible to detect which enemy entered your sword-area trigger via the OnTriggerEnter function. If I understand your setup correctly, this is what I would recommend:

  1. Set up a list (or HashSet) of whatever type of script your enemies have attached. Alternatively, you could simply have a list of Transforms. This will serve as a record of all the enemies that are currently in attacking range.
  2. In your OnTriggerEnter function, check if the collider that your sword-area detected is an enemy, either using tags or checking for the presence of a script.
  3. If you did collide with an enemy, add the enemy to the list you made in step 1.
  4. In your OnTriggerExit function, check if the departing collider is in your list of enemies. If it is, remove it from the list.
  5. When you swing your sword, apply damage to all the enemies in your list.

I strongly recommend using a HashSet because of the efficiency of the Contains and Remove functions.

It should be noted that in order for your sword-area to detect the presence of an enemy, it will need to have a Rigidbody attached to it or one of its parents. Check this collision matrix to make sure that the colliders you are dealing with will send messages to one another.