- Home /
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!
I don't know how this would work in practice, but you could provide a deflective force selected from a random range to each shot. The higher the agility, the higher the range.
Answer by Tarlius · Feb 25, 2013 at 04:58 AM
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).
Answer by LivingUniverse · Feb 25, 2013 at 07:18 AM
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);
}
}
I would advise against using a singleton to implement the logic, because the obvious next step is to implement the player attacking something else, which will mean repeating yourself. This will also make it more difficult to implement buffs/debuffs to accuracy/avoidance, if desired at a later date.
Also, note that your evasion stat could get very broken. High level enemies will have no way to increase their accuracy, and a player who spends a lot of points in agility will be virtually unhittable. (this is a common problem in $$anonymous$$$$anonymous$$Os, where a lot of the time the "only" "solution" is to make "harder" enemies just hit ridiculously hard. Then gameplay boils down to praying you don't get hit twice in a row before you can heal.)
This will of course depend on how much avoidance you will allow the player to have, but definitely something to bare in $$anonymous$$d!
(Oh, and you should post comments as comments, not answers)
hey, about the way i chose to do the evasion; on higher level enemies i can just give them a bigger range, as for the player im not going to give them much evasion overall. (i didnt realise that i could post answers and comments separately :/ )
I just thought I'd raise it as a potential issue because in your example it was a hardcoded value :)
Your answer
Follow this Question
Related Questions
Leveling up in RPG 1 Answer
Opinion on this level upgrading system? 0 Answers
How To Make A Simple MMO? 3 Answers
How to make a cutscene like RPG Games 1 Answer
Top-down 2d Tactical RPG 0 Answers