Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by TwinPrime · Feb 26, 2018 at 12:08 PM · script.unity 2dunityeditoroptimizationplatformer

Which is best for optimization

Hi guys, currently working on a 2D platformer like survival. There will be lots of Enemies etc. so I had this question like for 2 days, couldn't find any answers by myself.
Which is best to apply Damage and manage Health of the Enemies. I have this two types of Methodes;


Deleting Unity. (joking) Creating a one Methode called Health Manager, It will loop and get all the enabled enemies from the Scene, Has a List of Stat for all indv. Enemy , long story short, one method will loop and apply damage (with doing the math)

or

Every Enemy will have ApplyDamage method and I will call them like SendMessage("ApplyDamage "); for those who is in collider etc.

Comment
Add comment
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

1 Reply

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

Answer by Xarbrough · Feb 26, 2018 at 12:54 PM

My advice would be to not think about performance optimizations at all right now. It doesn't matter unless you are shipping a game and you have profiled and identified specific bottlenecks which prevent your game from achieving some specific platform requirements.

However, you should think about which code solution is more maintainable or "just works better" for you as the developer. I would not use SendMessage because it is not type safe and cannot be debugged as easily as other methods. For example, the receiving message is not compile-checked in any way, so you could accidentally delete it, and if you haven't selected 'requiresReceiver' you won't even get an error. Overall, the system is quite old and not recommended anymore.

For starters, I would create a "Health" component which implements a "IDamageable" interface. Enemies carry a weapon or some sort of "DamageDealer", which can check via collision (OnTriggerEnter) if another IDamageable component is within the trigger. If so, apply the damage. No need for a Health Manager in my opinion. I assume "lots of enemies" is less than 100. Dealing with hundreds of enemies might require a different approach, but I wouldn't tackle this issue without having tried the first version successfully.

There's a related Unity tutorial about interface here.

And here is some code:

 using UnityEngine;
 
 public class Health : MonoBehaviour, IDamageable 
 {
     public int maxHealthPoints;
 
     private int healthPoints;
 
     private void Start()
     {
         // Start at full health.
         healthPoints = maxHealthPoints;
     }
 
     public void ChangeHealth(int amount)
     {
         healthPoints += amount;
 
         if(healthPoints < 0)
         {
             Debug.Log("x.x");
         }
     }
 }
 
 public interface IDamageable
 {
     /// <summary>
     /// Damage is applied by a negative amount,
     /// Healing is applied by a positive amount.
     /// </summary>
     void ChangeHealth(int amount);
 }
 
 public class Enemy : MonoBehaviour
 {
     public int damage = 5;
 
     void OnTriggerEnter(Collider other) 
     {
         IDamageable damageable = other.GetComponent<IDamageable>();
         if(damageable != null)
         {
             damageable.ChangeHealth(-damage);
         }
     }
 }

Remember to put both MonoBehaviour classes in their own script files called "Health.cs" and "Enemy.cs". The interface can be defined in its own file as well, if you want, or leave it in Health. I assume you know how collider triggers work (the "isTrigger" option on the collider, and that one of two colliders must have a rigidbody attached).

Finally, I recommend using integers for health and not floats. Ints are much easier to compare (e.g. apply a buff only if at full health). If you want more resolution, you can just let the range go from 0 - 10000 and then convert down to 0 - 100 resolution for UI display. Games like Diablo model their characters health as integers from zero to a couple of million health points.

Comment
Add comment · Show 1 · 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 TwinPrime · Feb 26, 2018 at 02:30 PM 0
Share

well explained, i created a BaseAttack class, with some attackTypes., every different AttackType class Overrides the Base Attack virtual attack() method. so, I put some ienumerator to control the "per second damage or I don't know, healing" thing. so no need to put manager u said. Okay then, In attack func. I'll create a circle collider 2d. and I will attach on every enemy the enemy.cs etc. I'll try,

Thank you :)

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

how to make scripts show gizmos in the Gizmos menu? 1 Answer

The minSdk version can not be specified in the AndroidManifest.xml file. You have to remove it. (See the Console for details) 0 Answers

Particle System Won't Play At All 0 Answers

Object disappears on running 1 Answer

Can not double jump when i fall off platform 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