Hi everyone,
I’m having a few problems with a damage system I’m building for a platformer. In the game you can hit something with different types of damages listed like this
public enum DamageType {Pierceing, Slashing, Fire, Bludgoening, Ice, Lightning, none};
Then I have two scripts:
Damage Source:
public class DamageSource : MonoBehaviour {
public int damageDealt;
public DamageType damageTypeDealt;
public int GetDamageAmount() {
return damageDealt;
}
public DamageType GetElementType() {
return damageTypeDealt;
}
}
And DamageReceiver:
public DamageType weakness;
public DamageType immuneTo;
void OnTriggerEnter2D (Collider2D col)
{
DamageSource damageGiver = col.GetComponent<DamageSource>();
if (damageGiver)
{
if (damageGiver.damageTypeDealt != immuneTo)
{
currentHP -= damageGiver.damageDealt;
StartCoroutine(CheckLife());
}
}
}
This is working fine. What I can’t get my head around is how to make this work if the target is immune to MORE than one type of damage and also, what if the damage giver has more than one type of damage?
Does anyone have an idea of what could I do? I’m really stuck with this…
Sorry for my bad english…
public DamageType immuneTo; //make this an array and assign one or more types
Then in the ontrigger method check if array contains “damageTypeDealt”:
if (immuneTo.Contains(damageGiver.damageTypeDealt))
{
currentHP -= damageGiver.damageDealt;
StartCoroutine(CheckLife());
}
If the “Contains” method is not showing up put this at the start of the script:
using System.Linq;