• 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 MDarkwing · Jun 30, 2014 at 02:26 PM · damagereduction

Damage System - How to reduce damage properly?

Well the whole subject is much more complex than the headline states so I'll try to explain it as best as I can.

I have a Unit class, AttackMissile class and UnitManager class. Unit class call coroutine on AttackMissile class which moves the projectile toward target, when it reaches the target it should deal damage.

UnitManager class contains bunch of event that are embedded in methods to be called correspondingly to recreate some states that unit passes (Unit attacks / unit is attacked, unit deals damage / unit takes damage and etc) so I can attach different spells effect to this states. I'll take Dota's Axe for example and his passive, which triggers on UnitIsAttacked event. Let's say that for my example I want a spell that reduces damage by some % when UnitTakesDamage. In TestSpellClass I'll add a reference to which unit is monitored (Axe in our case) and i will subscribe the method that performs all about spell to the UnitTakesDamageEvent in UnitManager class by calling method UnitManager.Instance.UnitTakesDamageTrigger(some parameters...) which does following

 public void UnitTakesDamageTrigger(some parameters...)
 {
     if(UnitTakesDamageEvent != null)
         UnitTakesDamageEvent.Invoke(some parameters...)
 }

And now comes the tricky part. If I want to deal damage in the end of coroutine that was called on the AttackMissile class, so my missile has reached target and now I need to deal damage, I call the method to invoke all effects that would affect the damage to be changed (in this case test ability), how can I do that? I can't pass objects by reference to the events or either have events with return type, I get that. What if that same damage is affected by let's say bonus 50 dmg, then % reduction of 15% and flat reduction of 15 dmg. That bring me to conclusion that i should have also damage handling class, DamageHandleClass where I would have some sort of array that hold all modifications of that damage, that performs those modification and them returns or deals that damage.

My question is am I thinking in right direction, and has anybody else stumbled upon same problem. I'm not asking for code solution just an idea which now I currently lack totally.

Thank you in advance.

Comment
Add comment · Show 4
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 Owen-Reynolds · Jun 30, 2014 at 04:13 PM 0
Share

This is just a standard programming issue. The way you'd do it in Unity is the same as how you'd do it anywhere else. It sounds like the sorts of things done in $$anonymous$$$$anonymous$$O's, with complex chains of damage modifiers (which always have bugs.)

The only Unity part, I think, is how you send damage through OnCollisionEnter. Just give the missile a "damageType" script, and have OCE grab it. It can even have a pointer to the firer, so you can get a list of damage modifiers.

avatar image MDarkwing · Jul 01, 2014 at 09:53 PM 0
Share

Well this is not a programming issue, it's more of a logic or how the system is designed. I've done it today, just as my initial thought was, I have an array for bonus dmg table and an object which stores data about damage bonus (flat or not, amount, percentage etc) and I can easly extend it toward my needs.

Also whole thing is wrapped with many events (aka triggers) on which I can easily attach any kind of neat effects/spells/abilites etc, works like a charm. The only problem I had was is multiple access to some class field (in my case damage) but since I implemented array data storing this issue has gone (I'm actually using list). I'll explain more detaily what I've done if anyone stumbles upon this post, I'm too tired right now.

Thanks for your insights anyway man. Respect

avatar image Owen-Reynolds · Jul 01, 2014 at 11:47 PM 1
Share

Logic and data-structure/class design are what programming is all about. At least, that's what I learned in most of my classes.

The stuff UnityAnswers tends to help with, are specific things unique to Unity: layers vs triggers & raycasts; how to fill in a regular class using the Inspector; using Send$$anonymous$$essage ... .

"How would I design" are more for the forum (which I don't much care for.) And even then, they tend to be specific Unity problems.

avatar image MDarkwing · Jul 02, 2014 at 07:09 AM 0
Share

Programming is paradigm used to solve those logic problems because any kind of problem can be brought down to a mathematical structure or object whatever you call it, then solved using programming. But that's just my point of view, I study math so I tend to be geeky sometimes xD Anyway cheers, gonna sum up the answer now.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MDarkwing · Jul 02, 2014 at 07:13 AM

So just as my original idea was, I have a list of objects that hold all the reductions and bonuses for damage and one method that calculates the damage. Same thing can be applied to let's say attack speed, or some other stats (strength, agility, intelligence, armor whatever stats you have in your game)

Objects hold info about that damage I called the class BuffDamage (has: int id, float damageAmount, bool flat). Hope someone will find this usefull.

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet scripting , i need help 1 Answer

Question about models ... 1 Answer

how do i make a script that calculate how much damage did i loss 1 Answer

For loop on collision (two hit kill) 2 Answers

How can I make a gun shoot and apply damage to the target? 0 Answers


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