- Home /
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.
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.
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.
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
Follow this Question
Related Questions
Finding a certain variable is failing 0 Answers
Area of effect 1 Answer
Two objects with different damage - Not working? 0 Answers