It wasn’t clear to me that the target
for CanAttack()
was meant to be the potential target that we just got from the hit
list of the RayCast. At first I had the issue that I couldn’t hit any enemy anymore because CanAttack()
couldn’t possibly be true
without having a valid target, but if it wasn’t then it would always skip to the next item of the hit
list.
I solved that by first assigning and then check in CanAttack()
and if not (because the potential target was dead already) skip over to the next item, but that felt wrong, too.
Now, with the solution from the lesson I would rather go one step further and, since it doesn’t depend on any state within the player’s Fighter
component, turn it into a static
method and call it as Fighter.CanAttack(potentialTarget);
instead.
as for its implementation, I simply added a null
guard to getting the Health
component from the combatTarget
, so if it’s null
the code will never attempt to grab the health and targetToTest
will be null
just the same.
public static bool CanAttack(CombatTarget combatTarget)
{
Health targetToTest = combatTarget?.GetComponent<Health>();
return (null != targetToTest && !targetToTest.IsDead());
}