Help implementing player dodge or enemy miss rate

Hi people, Im not really looking for any code(although that would be really helpful) but i was wondering how to implement dodging for my player and im not talking about animations, im just talking about when the enemy attacks me he has a chance to miss based on my agility attribute.

I have an RPG leveling and attribute system already implemented which works and i have the players attack based on how much damage he has from his ‘power’ atttribute but i also have an agility attribute and i was wondering what way or forumla i could use to make the enemy miss/player dodge.

(i also have a defence attribute and it would be nice if anyone could give an idea on how i should reduce the enemies attack based on the defence number)

thank you!

Just program it without the evade (when you get hit, apply damage from an enemy), then use the stats of the enemy to work out the damage. Then, add in logic for evasion that skips the damage if certain conditions are met.

public enum Result {
    Miss, Hit, Crit, 
}
public AttackResult Attack(Attacker attacker) {
    if(!IsHit(attacker) {
        return Result.Miss;
    }
    return GetDamageResult();
}
bool IsHit(Attacker attacker) {
    return (attacker.Dexterity - this.Agiity) >= Random.Range(-10, 10); // Simple example
}

If you need to animate the result, you can use the returned AttackResult enum (hit/crit/miss/etc). What level of animation you want will depend on the style you’re going for. A lot of RPG don’t really bother animating misses, Final Fantasy always has the sword go through the actor and just displays “Miss”. Other games might make the actor insta-parry or something.

On a side note, I would be cautious about pre-calc’ing everything (if thats your intent). A project at my company ran into problems when the spec changed and they wanted to add a “tap to crit” sort of thing (similar to Squall’s gunblade in Final Fantasy 8).

oh hey, thanks for the answers. Both of you gave me an idea, I think this would work:

private int rand1 = Random.Range(0, 101);

private int rand2 = Random.Range(0,(Players Evasion*))

if(rand1 <= rand2){
  //enemy damage = 0
} else if (rand1 > rand2){
  //enemy's normal damage here
}

*players evasion is -
(int)Player.Instance.GetStats((int)StatName.Evasion).AdjustedValue

that code refers to the stat that is increased when the player up’s the agility attribute.
(in my code)

each few points in agility increases evasion by 1, so that if the player has 10 evasion it would be like 10% that the enemy will miss.

EDIT!!! : i just realised something. Incase anyone reads my code and wants to try it, i realised that i made a mistake. You dont want to have the players evasion as a random range! or it will be randomly changing each time you get hit. This is how i did it with crit chance (which uses the same principle):

private int rand; 
private int critChance;

private void MeleeAttack(){
		float distance = Vector3.Distance(target.transform.position, transform.position);
		if(distance < 2.8f){
			EnemyHP eh = (EnemyHP)target.GetComponent("EnemyHP");
			rand = Random.Range(0, 101);
		    critChance = (int)PlayerChar2.Instance.GetAbilities((int)AbilityName.Critical_Chance).AdjustBaseValue;
			if(rand <= critChance){
				eh.AdjustCurrentHP(-(int)PlayerChar2.Instance.GetAbility((int)AbilityName.Attack).AdjustBaseValue * 2);
			} else if(rand > critChance){
				eh.AdjustCurrentHP(-(int)PlayerChar2.Instance.GetAbility((int)AbilityName.Attack).AdjustBaseValue);
			}
			Debug.Log(rand +"/"+critChance);
		}    
	}