Hello everyone, I am working on a game that based pretty much entirely melee combat but I’m running into a few issues that are making my melee combat system inefficient. In order to register hits I am currently using OnTriggerEnter, this works well for attacks that are instant, however say the enemy is in the beginning of the melee animation but not actually swinging just yet it still registers damage even though its only in the beginning of the animation. Now I added timers to relieve this issue but now I have run into a second issue doing it this way. Basically I have a new animation where the player charges up an attack and the charge up time is about 2-3 seconds before the swing actually happens, in this period of time some of the enemies have already entered the trigger area and since they are not exiting and re-entering it does not register damage. Now the player can run back a bit and go back in to register the damage but its not how I’d like that to work.
Basically, what other methods besides OnTriggerEnter are good for registering damage? Are there other ways I can go about timers, or even my animations? OnTriggerEnter is working as intended, but for my game it leads to very funky hit registers since most of the animations have a slight charge up time. Thanks!
I solved my own issue after lots of research. If anyone is curious on what I did, I made two functions in my parent object that activate and deactivate the ability to do damage.
public void AllowDamage()
{
GetComponentInChildren<Ultimates>().allowDamage = true;
}
public void StopDamage()
{
GetComponentInChildren<Ultimates>().allowDamage = false;
}
I then found the frames in the animation that I wanted to start damage and stop damage and added an animation event on each of those frames, one that calls AllowDamage() and one that calls StopDamage(). I then used the Physics.OverlapSphere to detect when enemies were in range to do damage.
void Ultimate()
{
Collider[] hitBox = Physics.OverlapSphere(ultimateHitBox.transform.position, ultimateRange, enemy);
foreach (var box in hitBox)
{
box.GetComponent<EnemyController>().die();
}
}
Finally in my update I detected whether the animation was playing and whether damage was allowed and then called the function if both were true.
if (GetComponentInParent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Armature_Ultimate") && allowDamage)
{
Ultimate();
}
I hope this helps someone else, I basically had to pull from multiple resources to get what I wanted since most solutions out there are to just use colliders and OnTriggerEnter.
4 Likes