My equation returns infinity?

I’m trying to achieve a simple but effective formula for the skills that the player and ai use.
For my player i have this; Which works absolutely fine…

var probability = Random.Range(1 / ((player.Attack / opponent.defence) * 0.515), 0.0 );
	print(probability);
if(probability < 0.5){	
       /// Applydamage code///
}

But then you come to the ai code, which is the same as above. If i have 3 values higher than the ai’s attack it returns as infinity. The calculation works fine in a calculator and i cant seem to find any faults.

var probability = Random.Range(1 / ((ai.Attack / player.Defence) * 0.515), 0.0 );
    if(hitReady){
    	print(probability);
		if(probability < 0.5){
                      //damage code///
}

Am i doing something wrong? Is there a better formula?

Are ai.Attack and player.Defence integers? If so, change the code to this:

var probability = Random.Range(1 / ((ai.Attack as float / player.Defence as float) * 0.515), 0.0 );
    if(hitReady){
    	print(probability);
		if(probability < 0.5){
                      //damage code///
}

Warning: untested.

Infinity comes from divide by zero. You should check values for ai.Attack and player.Defence.
Another question is why are you calculating the min value for Random.Range? Is your probability a negative value? I found it hard to believe that one of the values ai.Attack or player.Defence can be negative values.

Thanks a bunch, got it working now :slight_smile: