I haven’t done this before but my best guess and the way I’d approach this is with colliders or colliders as triggers.
Put a collider (either a box or a capsule, my first instinct is box… but thinking it over, a flattened capsule I think is possible and if so, that would make a great sword collider) on each sword. Then make it a trigger by checking the box for it in the inspector.
Then
void OnTriggerEnter(Collider c)
{
if(c.gameObject.tag == "enemySwordTag")
{
// we have a hit, do your response
}
}
There are at least two beautiful things here… one: you’re using the built in systems… you always want to use the built in systems… unless you really really know what you’re doing and can like write assembly code in your sleep or something
Second, you’re only checking this stuff when your character’s sword actually hits something. That’s a nice savings in processing power over some update method or whatever you might use.
Third (found it you have access to the enemy’s collider on his sword, through it with c.gameObject, you have access to every part of the enemy. You can call his scripts with c.gameObject.GetComponent<scriptName>().CallSomeFunction();
The most questionable thing about my code/suggestion that you might want to research first is whether to check the collider.gameObject’s tag… or find a better method. Tags seem to be for general categories… you might want to check some id that you’ve defined instead. Depends on how many enemies you’re going to have. I haven’t had a chance to explore this yet so I don’t know the options/benefits/negatives.
If you need to update continually that you are still colliding, then use OnTriggerStay
Or what you can do is set a boolean flag to true when you get OnTriggerEnter and false when OnTriggerExit happens.
I’m not sure which is the best method.
OnTriggerEnter happens when the colliders hit. OnTriggerStay happens almost every frame if the colliders collide. And OnTriggerExit happens when the colliders stop colliding.