Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by dorpeleg · Mar 12, 2013 at 02:18 PM · collideroverlapsphereaoehealth

Aura/AOE Problem

Hello everyone.

I'm trying to make a healing aura kind of a thing.

The aura should work like this:

There is a "healer" unit.

If any "friendly" units are in a certain distance from the "healer" then heal them with a certain amount every few second.

I know how to make the "healing" part, but I'm struggling with the aura part.

Here is what I got so far:

 private Collider[] Colliders;
     
     private float HealDistance = 3;
     
     // Update is called once per frame
     void Update () {
         Colliders = Physics.OverlapSphere(transform.position, HealDistance);
         for  (int i=0; i<Colliders.Length; i++){
             if (Colliders[i].tag == "Allies"){
                 Debug.Log(Colliders[i].name);
             }
         }
     }

The problem I have so far, is that the OverlapSphere is also returning the unit that is creating it.

And even after fixing that, I'm not sure whats the next step.

Thanks for your help.

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

2 Replies

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

Answer by dorpeleg · Mar 13, 2013 at 02:34 PM

I solved it.

The script below is placed on the healing unit and heals other units (not himself) every few secs.

 public class Healing : MonoBehaviour {
     
     private Collider[] Colliders;
     
     private int AmountToHeal = 5;
     private float curHealTime = 0;
     private float TimeBetweenHeals = 5;
     private float HealDistance = 3;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Colliders = Physics.OverlapSphere(transform.position, HealDistance);
         for  (int i=0; i<Colliders.Length; i++){
             if (Colliders[i].tag == "Allies"){
                 if (Colliders[i].gameObject != gameObject){ //if not hiting itself
                     Colliders[i].gameObject.GetComponent<BaseUnit>().isHealed = true;
                     HealUnit(Colliders[i].gameObject);
                 }
             }
         }
     }
     
     public void HealUnit (GameObject UnitToHeal){
         curHealTime += Time.deltaTime;
         if (curHealTime >= TimeBetweenHeals){
             Debug.Log("healing: "+UnitToHeal);
             UnitToHeal.GetComponent<BaseUnit>().Heal();
             UnitToHeal.GetComponent<BaseUnit>().Health += AmountToHeal;
             UnitToHeal.GetComponent<BaseUnit>().isHealed = false;
             curHealTime = 0;
         }
     }
 }

I still have a slight problem with the isHeald always staying true and the particle effects keeps playing... but I think I can find the bug myself.

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 Bunny83 · Mar 12, 2013 at 02:41 PM

Usually the easiest way is to use a prefab which get attached to the affected unit as long as it doesn't have one yet. This effect-prefab will take care of your effect (particle system, ...) and will destroy itself after a certain amount of time. This time should be "retriggerable". So each time the effect caster does it's check it does the following:

  • Check if the unit already has the prefab attached
    • If it has, retrigger the timeout

    • if not, create an instance of the prefab and attach it as child to the unit.

I wouldn't use Update in this case. It's usually enough to do this check every 0.5 seconds or every 0.1 if you want. The timeout should be at least double this interval time, so it never runs out while the unit is inside the affected range.

To check if the prefab is already attached you can use GetComponentInChildren with the script type that's part of your prefab. If it has one you can use the returned reference to retrigger the script, if not, create the prefab.

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 dorpeleg · Mar 12, 2013 at 03:12 PM 0
Share

Now that I think about it, maybe its best to put the heal script on the other units and not the healer, then check if the unit is close to a healer, then heal itself.

Then it might be easier to control the on/off part of the script.

I really prefer not to make more prefabs, and use more code if possible.

But this still doesn't solve my problem of OverlapSphere returning "itself".

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

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

Finding a certain variable is failing 0 Answers

Area of effect 1 Answer

Two objects with different damage - Not working? 0 Answers

overlapsphere to destroy NPCs on exit 1 Answer

OverlapSphere returned collider arrays 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