Damage Formula Not Working

I just finished making a damage formula for my game and wanted to implement it in place of a simple health -= damage that I was using before.

public bool TakeDamage(int pow, int lvl, int move)
    {
        currentHP -= Mathf.RoundToInt(pow * ((move + lvl) / defense));

        Debug.Log(Mathf.RoundToInt(pow * ((move + lvl) / defense)));
        Debug.Log(pow);
        Debug.Log(lvl);
        Debug.Log(move);
        Debug.Log(defense);

        if (currentHP <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

The problem is that it always outputs 0. I made sure that every individual input is correct but the final outcome always gives me a 0, and so no one can take damage. Does anyone see what I am doing incorrectly here?

Can you give an example of what values your debugs are printing out? Are you saying this method always returns true?

All of your inputs are integers so you’re doing integer division. Integers don’t have any fractions so any result of the division that is less than 1 will result in 0. Is that it? What are all the numbers that print?

2 Likes

Right, also even with integer division it may not matter when you don’t put the unnecessary brackets the way they are. Just doing the multiplication first and the division last should already help. Also if all 4 variables are integers (pow, move, lvl and defence) there would be no reason to round to int as when the calculation is done with integer math, the result would be an integer. So just doing this should probably work:

currentHP -= pow * (move + lvl) / defense;

Is there are concrete / good reason why you use integer values in the first place? Most games use floats for literally everything. Yes, you may round numbers for display but other than that, especially when you have a “more complex damage system” using floats would make much more sense.

2 Likes

Thanks for the

Gotcha, gotcha that is super helpful. I’m new to coding so my experience isn’t very deep, that’s the only reason I was using int instead of float, just never occurred to me. Thanks to everybody!