• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LivingUniverse · Feb 25, 2013 at 03:19 AM · rpgleveling

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 25, 2013 at 03:55 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
         }    
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tarlius · Feb 25, 2013 at 07:40 AM 0
Share

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)

avatar image LivingUniverse · Feb 25, 2013 at 08:08 AM 0
Share

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 :/ )

avatar image Tarlius · Feb 25, 2013 at 08:10 AM 0
Share

I just thought I'd raise it as a potential issue because in your example it was a hardcoded value :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Opinion on this level upgrading system? 0 Answers

Leveling up in RPG 1 Answer

How do I disable diagonal movement in a top down 2d rpg?,I'm trying to disable diagonal movement in a top down rpg 1 Answer

new user very easy c# question regarding syntax 1 Answer

Help with unity 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges