Is there a better way of coding this?

I have a bullet that can be one of 4 Types. Depending on which unit it hits the damage will be amplified or decreased.

Here is the code:

if(bullet.bulletType == Bullet.BulletType.AIR)
        {
            if(unitType == UnitType.AIR)
            {
                currentHP -= (int)bullet.damage;
            } else if(unitType == UnitType.WATER)
            {
                currentHP -= (int)bullet.damage * 2;
            } else if (unitType == UnitType.FIRE)
            {
                currentHP -= (int)bullet.damage;
            }
            else if (unitType == UnitType.EARTH)
            {
                currentHP -= (int)bullet.damage / 2;
            }
        } else if(bullet.bulletType == Bullet.BulletType.WATER)
        {
            if (unitType == UnitType.AIR)
            {
                currentHP -= (int)bullet.damage / 2;
            }
            else if (unitType == UnitType.WATER)
            {
                currentHP -= (int)bullet.damage;
            }
            else if (unitType == UnitType.FIRE)
            {
                currentHP -= (int)bullet.damage * 2;
            }
            else if (unitType == UnitType.EARTH)
            {
                currentHP -= (int)bullet.damage;
            }
        }
        else if (bullet.bulletType == Bullet.BulletType.FIRE)
        {
            if (unitType == UnitType.AIR)
            {
                currentHP -= (int)bullet.damage;
            }
            else if (unitType == UnitType.WATER)
            {
                currentHP -= (int)bullet.damage / 2;
            }
            else if (unitType == UnitType.FIRE)
            {
                currentHP -= (int)bullet.damage;
            }
            else if (unitType == UnitType.EARTH)
            {
                currentHP -= (int)bullet.damage * 2;
            }
        }
        else if (bullet.bulletType == Bullet.BulletType.EARTH)
        {
            if (unitType == UnitType.AIR)
            {
                currentHP -= (int)bullet.damage * 2;
            }
            else if (unitType == UnitType.WATER)
            {
                currentHP -= (int)bullet.damage;
            }
            else if (unitType == UnitType.FIRE)
            {
                currentHP -= (int)bullet.damage / 2;
            }
            else if (unitType == UnitType.EARTH)
            {
                currentHP -= (int)bullet.damage;
            }
        }

What is a better way of doing this?

Absolutely. Generally the first step is to avoid using enums as they will be nothing but pain moving forward.

Instead, you can use ScriptableObjects to define damage types. These types could also be used to define what is weak to them.

Could be as simple as:

[CreateAssetMenu(menuName = "Weapons/Weapon Element")]
public class WeaponElement : ScriptableObject
{
    [SerializeField]
    private List<WeaponElement> elementWeakness = new List<WeaponElement>();
   
    public bool IsWeakToElement(WeaponElement weaponElement)
    {
        return elementWeakness.Exists(weaponElement);
    }
}

(Typed out on Notepad++).

So you could have fields for these on bullets, and on units. The rest is pretty straight forward.

2 Likes

Thanks for the fast answer. I have never used Scriptable Objects before. Im gonna make a small test project and will try to implement it what you showed me. Hope I get it working.

Edit: .Exists gave an error, after googleing I ended up using contains and the error is gone