Weird Issue with Auto Attacking

It’s too late for me to actually debug this, and I’m sure I’m just missing where I’m looking to fix this, but I just implemented Sam’s Auto attack feature, and when the enemy is attacking the player, instead of auto attacking (unless the player initiated the combat, and does NOTHING else) the player kind of just stutter steps, and does nothing. Like it’s trying to do something but it’s not taking a target bc the target is null, so you’re still getting whacked by the enemies

Sam’s auto attack is only covering “I just finished killing Sam, so it’s time to move on to Frodo”, but doesn’t cover “Sam just attacked me while I was minding my own business trying to steal Frodo’s ring”.

For this, you need some way for Fighter to pick up on the fact that you’ve been attacked. This looks like a good place for an event in Health.

public event System.Action<GameObject> OnHitTaken;

Fighter would then subscribe to it’s Health’s OnHitTaken.

The tricky part is only switching targets when we don’t already have a target…

void OnHitTaken(GameObject attacker)
{
    if(target!=null && target.isAlive) return; //Don't bug me, I'm still killing Sam
    if(attacker.TryGetComponent(out Health newTarget))
    {
         Attack(newTarget);  //Yikes, guess I better steal Frodo's ring later, in the meantime let's kill the guy who attacked me.
    }
}

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms