Search Unity

Creating offset for Physics2D.OverlapCircleAll

Discussion in 'Scripting' started by DevAigina, Jul 15, 2018.

  1. DevAigina

    DevAigina

    Joined:
    Jun 6, 2017
    Posts:
    29
    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 ?
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. /// <summary>
    5. /// Clery is an enemy type that heals nearby allies while dying.
    6. /// </summary>
    7. class CleryController : BaseEnemy , IDeathrattle
    8. {
    9.     [ Header( "Clery Properties" ) ]
    10.     public int        healRate   = 0;
    11.     public float      healRadius = 0.0f;
    12.     public LayerMask  enemyLayer = 0;
    13.     public bool       isHealed   = false;
    14.  
    15.     /// <summary>
    16.     /// Movement logic for Clery.
    17.     /// </summary>
    18.     public override void Movement()
    19.     {
    20.         if( playerInstance != null )
    21.         {
    22.             transform.position = Vector2.MoveTowards( ( new Vector2( transform.position.x , transform.position.y ) ) , ( playerInstance.transform.position ) , ( Time.deltaTime * movementSpeed ) );
    23.         }
    24.     }
    25.  
    26.     /// <summary>
    27.     /// Scans the radius to find allies to heal.
    28.     /// </summary>
    29.     private void ScanAllies()
    30.     {
    31.         const float offset = 1.0f;
    32.         Vector2 healPos    = new Vector2( ( transform.position.x + offset ) , ( transform.position.y + offset ) );
    33.         Collider2D[] colls = Physics2D.OverlapCircleAll( healPos , healRadius , enemyLayer );
    34.  
    35.         // If there is nearby ally.
    36.         if( colls.Length > 0 )
    37.         {
    38.             Heal( colls );
    39.         }
    40.         else
    41.         {
    42.             Debug.Log( "Found no allies to heal !" );
    43.         }
    44.     }
    45.  
    46.     /// <summary>
    47.     /// Heals nearby allies with given heal rate.
    48.     /// </summary>
    49.     /// <param name="allies"> The allies near to Clery. </param>
    50.     private void Heal( Collider2D[] allies )
    51.     {
    52.         if( !isHealed )
    53.         {
    54.             BaseEntity tmp = null;
    55.             int i;
    56.             for( i = 0 ; i < allies.Length ; i++ )
    57.             {
    58.                 tmp      = allies[ i ].GetComponent< BaseEntity >();
    59.                 if( tmp != null )
    60.                 {
    61.                     tmp.AddHealth( healRate );
    62.                     EffectManager.efM.CreateHealEffect( allies[ i ].transform.position );
    63.                 }
    64.             }
    65.            
    66.             isHealed = true;
    67.         }
    68.     }
    69.  
    70.     /// <summary>
    71.     /// Deathrattle logic for Clery. ( Will heal near allies ) ( IDeathrattle -> Deathrattle )
    72.     /// </summary>
    73.     public void Deathrattle()
    74.     {
    75.         // Heal nearby allies here !
    76.         ScanAllies();
    77.     }
    78. }
    Thanks for any help. :)
     
  2. DevAigina

    DevAigina

    Joined:
    Jun 6, 2017
    Posts:
    29
    I fixed this by using : https://answers.unity.com/questions/195992/overlap-sphere-ignore-myself.html
    this post.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. /// <summary>
    5. /// Clery is an enemy type that heals nearby allies while dying.
    6. /// </summary>
    7. class CleryController : BaseEnemy , IDeathrattle
    8. {
    9.     [ Header( "Clery Properties" ) ]
    10.     public int        healRate   = 0;
    11.     public float      healRadius = 0.0f;
    12.     public LayerMask  enemyLayer = 0;
    13.     public bool       isHealed   = false;
    14.  
    15.     /// <summary>
    16.     /// Movement logic for Clery.
    17.     /// </summary>
    18.     public override void Movement()
    19.     {
    20.         if( playerInstance != null )
    21.         {
    22.             transform.position = Vector2.MoveTowards( ( new Vector2( transform.position.x , transform.position.y ) ) , ( playerInstance.transform.position ) , ( Time.deltaTime * movementSpeed ) );
    23.         }
    24.     }
    25.  
    26.     /// <summary>
    27.     /// Scans the radius to find allies to heal.
    28.     /// </summary>
    29.     private void ScanAllies()
    30.     {
    31.         //const float offset = 1.0f;
    32.         Collider2D[] colls = Physics2D.OverlapCircleAll( transform.position , healRadius , enemyLayer );
    33.  
    34.         // If there is nearby ally.
    35.         if( colls.Length > 0 )
    36.         {
    37.             Heal( colls );
    38.         }
    39.         else
    40.         {
    41.             Debug.Log( "Found no allies to heal !" );
    42.         }
    43.     }
    44.  
    45.     /// <summary>
    46.     /// Heals nearby allies with given heal rate.
    47.     /// </summary>
    48.     /// <param name="allies"> The allies near to Clery. </param>
    49.     private void Heal( Collider2D[] allies )
    50.     {
    51.         if( !isHealed )
    52.         {
    53.             BaseEntity tmp = null;
    54.             int i;
    55.             for( i = 0 ; i < allies.Length ; i++ )
    56.             {
    57.                 tmp      = allies[ i ].GetComponent< BaseEntity >();
    58.                 if( tmp != null && allies[ i ].gameObject.transform.root != this.gameObject.transform )
    59.                 {
    60.                     if( tmp.currentHealth < tmp.maxHealth )
    61.                     {
    62.                         tmp.AddHealth( healRate );
    63.                         EffectManager.efM.CreateHealEffect( allies[ i ].transform.position );
    64.                     }
    65.                     else
    66.                     {
    67.                         Debug.Log( "I can not overheal my allies !" );
    68.                     }
    69.                 }
    70.                 else
    71.                 {
    72.                     Debug.Log( "I can not heal myself !" );
    73.                 }
    74.             }
    75.            
    76.             isHealed = true;
    77.         }
    78.     }
    79.  
    80.     /// <summary>
    81.     /// Deathrattle logic for Clery. ( Will heal near allies ) ( IDeathrattle -> Deathrattle )
    82.     /// </summary>
    83.     public void Deathrattle()
    84.     {
    85.         // Heal nearby allies here !
    86.         ScanAllies();
    87.     }
    88. }