How do you Isolate Physics2D.OverlapArea to a single script/gameObject/Instance?
I am currently using 2DOverlapArea to return true in order to tell if an enemy is directly in front of the player. It works and allows me to apply damage, but the problem is it affects every instance of that enemy in the game and not just the single enemy overlapping in the 2D area.
////////////////////////////- FIRE BREATH -////////////////////////////////////////////////////////
// Is on Fire checks to see if the skeleton is in the firebreath area in front of the player dragon
// and if the player is pressing the fire breath key.
// Physics2D.OverlapArea draws a rectangle from an upper left corner to a lower right corner
// Finds the GameObjects that correspond to firebreath, and uses the two to draw a rectangle
// EnemyLayer is a layer mask so that only enemy objects trip it
this.isOnFire = Physics2D.OverlapArea(GameObject.Find("FireBreathRectUpperLeft").transform.position, GameObject.Find("FireBreathRectLowerRight").transform.position, EnemyLayer);
if (this.isOnFire)// Is the skeleton in the firebreath area?
{
if (Input.GetButton ("Fire2") == true) { // Is the player firebreathing?
this.ApplyDamage (1);
SpriteRenderer Renderer1 = GetComponent<SpriteRenderer> ();
Renderer1.color = new Color (1.0f, 0f, 0f, 1f);
}
if (Input.GetButton ("Fire2") == false) { // If not, return the skeletons color
SpriteRenderer Renderer1 = GetComponent<SpriteRenderer> ();
Renderer1.color = new Color (1.0f, 1.0f, 1.0f, 1f);
}
}
if (this.isOnFire == false) { // Double check to make sure color goes back to normal
SpriteRenderer Renderer1 = GetComponent<SpriteRenderer> ();
Renderer1.color = new Color (1.0f, 1.0f, 1.0f, 1f);
}
What I am trying to do is to isolate the damage/sprite color change to the single enemy that is within the 2D Overlap area (which is what I thought would happen by referencing 'this'). Currently all enemies using this script get damaged, turn red, and get killed at the same time. My hope was that this would be simpler and more effective (for an area of effect weapon) than raycasting.
Notes: - ApplyDamage exists in the same script as the code above and is a public bool - isOnFire is a Private Bool variable - EnemyLayer is a layer they all share (All enemies are on this layer for referencing) - None of the enemies are affected if there are zero enemies in the overlap area, and all enemies are affected if one is in the overlap area
Thanks!