This is more of a math question. I want to understand how percentage calculation works, for something like crit chance, hit chance or item drops.
Is this code here roughly correct? Any feedback on how to do it better/smarter/more in depth? I'm pretty terrible at math, so any "probability calculation for idiots", especially on how to do it in Godot specifically, is greatly appreciated.
var hit_chance = 0.60 # hit chanc would be 60%
var evasion_boost = 0.2 # evasion chance would be 20%
var hit = false
func _ready():
calculate_hit()
func calculate_hit():
for i in 5:
var evasion = randf()
evasion += evasion_boost # add evasion boost to evasion
if evasion <= hit_chance:
hit = true
else: hit = false
EDIT: on second glance, i'm not sure what i was trying to accomplish with that code above. Here's better code. I think?
var hit_chance = 0.90 # hit chance is 90%
var evasion_boost = 0.2 # evasion chance is 20%
var hit = false
func _ready():
calculate_hit()
func calculate_hit():
var chance = hit_chance - evasion_boost # subtract evasion from hit chance
var percentage = chance * 100
print("hit chance is ", str(percentage), "%")
var RNG = randf()
print("RNG is ", RNG)
if RNG >= chance:
print("you hit!")
else: print("you missed!")