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*.tag == "Allies"){*

_ Debug.Log(Colliders*.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.

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.

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*.tag == "Allies"){*

_ if (Colliders*.gameObject != gameObject){ //if not hiting itself*_
_ Colliders*.gameObject.GetComponent().isHealed = true;
HealUnit(Colliders.gameObject);
}
}
}
}*_

* public void HealUnit (GameObject UnitToHeal){*
* curHealTime += Time.deltaTime;*
* if (curHealTime >= TimeBetweenHeals){*
* Debug.Log("healing: "+UnitToHeal);*
* UnitToHeal.GetComponent().Heal();*
* UnitToHeal.GetComponent().Health += AmountToHeal;*
* UnitToHeal.GetComponent().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.