Hello everyone ,
I am trying to create an enemy that heals nearby allies when dying.
To achieve this i am trying to get collisions via Physics2D.OverlapCircleAll
I don’t want to healer enemy heal itself , i only want to heal nearby allies.
So i created offset value for this. But it is not working.
Healer enemy keeps healing itself , how can i make ignore itself ?
using UnityEngine;
/// <summary>
/// Clery is an enemy type that heals nearby allies while dying.
/// </summary>
class CleryController : BaseEnemy , IDeathrattle
{
[ Header( "Clery Properties" ) ]
public int healRate = 0;
public float healRadius = 0.0f;
public LayerMask enemyLayer = 0;
public bool isHealed = false;
/// <summary>
/// Movement logic for Clery.
/// </summary>
public override void Movement()
{
if( playerInstance != null )
{
transform.position = Vector2.MoveTowards( ( new Vector2( transform.position.x , transform.position.y ) ) , ( playerInstance.transform.position ) , ( Time.deltaTime * movementSpeed ) );
}
}
/// <summary>
/// Scans the radius to find allies to heal.
/// </summary>
private void ScanAllies()
{
const float offset = 1.0f;
Vector2 healPos = new Vector2( ( transform.position.x + offset ) , ( transform.position.y + offset ) );
Collider2D[] colls = Physics2D.OverlapCircleAll( healPos , healRadius , enemyLayer );
// If there is nearby ally.
if( colls.Length > 0 )
{
Heal( colls );
}
else
{
Debug.Log( "Found no allies to heal !" );
}
}
/// <summary>
/// Heals nearby allies with given heal rate.
/// </summary>
/// <param name="allies"> The allies near to Clery. </param>
private void Heal( Collider2D[] allies )
{
if( !isHealed )
{
BaseEntity tmp = null;
int i;
for( i = 0 ; i < allies.Length ; i++ )
{
tmp = allies[ i ].GetComponent< BaseEntity >();
if( tmp != null )
{
tmp.AddHealth( healRate );
EffectManager.efM.CreateHealEffect( allies[ i ].transform.position );
}
}
isHealed = true;
}
}
/// <summary>
/// Deathrattle logic for Clery. ( Will heal near allies ) ( IDeathrattle -> Deathrattle )
/// </summary>
public void Deathrattle()
{
// Heal nearby allies here !
ScanAllies();
}
}
Thanks for any help.