Damaging others enemies

I think I want to be able to add an option for a projectile (like in DiabloII amazon) to damage other enemies when flying through the target or hitting then before the target. I think I’ll try by checking other.CompareTag() and add some special tags to the enemies I want the option to work with.

M. D.

The easiest thing to do for this is to simply damage all Health components the projectile encounters…

void OnTriggerEnter(Collider other)
{
    if(other.gameObject == damageDealer) return; //avoid falsely hitting shooter 
    if(other.TryGetComponent(out Health health)
    {
         health.TakeDamage(damage, damageDealer);
     }
    if(other==target) Destroy(gameObject);
}

Thank you much for your response.

Good thing to avoid hitting ourselves.
I didn’t know about TryGetComponent, always learning more and more.

Brian you are reallly great help.

M. D.

Yes, TryGetComponent is a newer feature. It returns true if a component is found and false if not. If true, then the outed parameter is guaranteed to be non-null within the if() codeblock in which it is present.

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

Privacy & Terms