Hey everyone, I’ve been working on a turn based game and I just needed the enemy’s defense to reduce some of the player’s damage and then apply the result to the enemy’s health. The enemy loses their defense like they’re meant to, but the damage doesn’t carry over to their health.
“result” is the player’s original roll and “playerDamage” is meant to be the left over damage after the defense subracts from the original roll. I’ve tried a couple of methods and reorganizing the script, but I’m still pretty inexperienced, so I’ve hit a personal dead end.
The math you have doesn’t really make sense to me. Step through it line by line and think about exactly what each line is doing.
You’re reducing currentDefense by result. you are notably not checking if this makes currentDefense negative
I’ll assume this is a UI thing
you’re calculating the amount of damage to apply to the player as result - maxDefense. This doesn’t make sense to me for two reasons. First, you are just directly using the result variable again, ignoring any amount of damage that had already been applied to the defense stat. Second you’re subtracting maxDefense from it. I would expect you to subtract the amount of leftover damage that there would be after removing all defense.
You’re removing playerDamage from the player’s life.
Don’t be afraid to introduce intermediate variables into the calculations to make things easier for yourself.
Okay, so to answer each of your steps because I realize I didn’t explain some things.
I’m currently using if scenarios in the Update to lock the enemy’s defense to it’s minimum and maximum, but you’re right that it should be in this function; I’ve just been focused on this problem at the time and didn’t assume it could cause issue.
This is UI, yes.
I’m trying to subtract the max defense from the result and apply it to the enemy’s life because the enemy’s defense is always reset to max once each of the player’s attack is over. The currentDefense stat is simply to show the player whether or not they broke through armor.
playerDamage is just the name for how much damage the player is actually doing to the enemy.
If it would be more helpful for me to post the whole script, I can, I just didn’t want to cause too much visual clutter.
“result” is just one attack. Down the line, it may be the sum of however many dice the player can roll for their attack, but for now it’s just one randomly generated value. It will only ever be the result of one attack right now.
You are treating result as both the total for the whole turn and the incremental value. You should be damaging by the amount that is not covered by the currentDefense, not the maxDefense.
That’s not the issue. Like I said above, the defense will always be maxed at the time the player attacks. This is why I was subtracting maxDefense from result and making the sum of those values into playerDamage.
The problem is that no matter what formula I’ve used (including yours), the LoseLife function doesn’t want to do anything. It worked when I wasn’t using LoseDefense, but now it doesn’t.
Should I just make LoseLife it’s own script and try to call it from there?