Calculations Not Working (Help)

I can’t figure out why at run-time this code isn’t working. I’m using the following code to calculate damage:

public int CalculateAttackDamage(BaseMove usedMove, int level, int attack, int defense, Types.TypesList attackersType01, Types.TypesList attackersType02,
                                     Types.TypesList targetType01, Types.TypesList targetType02, int attackersBaseSPD){
        baseDamage = usedMove.MovePower;
        SetModifier(usedMove.MoveType, attackersType01, attackersType02, targetType01, targetType02, attackersBaseSPD);
        return (int)((((2 * level + 10) / 250) * (attack / defense) * baseDamage + 2) * modifier);
    }

And this is where I’m calling that method to set the damage to be applied:

if(playerUsedMove.MoveCategory == MoveCategories.MoveCategoriesList.PHYSICAL){
                totalPlayerDamage = battleCalcScript.CalculateAttackDamage(playerUsedMove, playerLVL, playerCurATK, enemyCurDEF, playerType01, playerType02,
                                                                           enemyType01, enemyType02, playerBaseSPD);
            }

The problem I’m having is that at run-time it’s always returning 1. I’ve done several Debug.Log’s to see if it’s actually retrieving the correct values for the variables in the parameters and it is…it’s just always calculating to 1 no matter what.

My first guess would be because you’re doing a lot of integer division. Try casting 250, attack, and defense to float.

1 Like

That seems to have done the trick. Thanks a lot @KelsoMRK . So whenever you’re going to divide with an integer you should cast it as a float?

integer division will give you result that you don’t always expect, especially with large denominators
(2 * level + 10) / 250 is probably evaluating to zero every time