- Home /
How to detect enemies inside an area or circle?
Hello, im doing my first game and im trying to detect the enemies inside an circle area around my player. I have two problems right now
-When a start the game, the circlecollider and player collider is detected as enemies even when i use the compare tag "Enemy"
-My corroutine dont refresh every 2s, and only detect colliders one time when the game start
public class ItemDamage : MonoBehaviour
{
[SerializeField] int damage;
[SerializeField] Collider2D[] objectsInsideArea;
Vector2 radiusOfDamage;
int radius;
public void Start()
{
radiusOfDamage = new Vector2(radius, 0f);
StartCoroutine(DamageEnemy());
}
bool IsEnemy(string tag)
{
for (int i = 0; i < objectsInsideArea.Length; i++)
if (objectsInsideArea[i].gameObject.CompareTag("Enemy"))
{
Debug.Log("object {i} is an Enemy");
return true;
} else
{
Debug.Log("object {i}");
}
return false;
}
IEnumerator DamageEnemy()
{
objectsInsideArea = Physics2D.OverlapAreaAll(Vector2.zero, radiusOfDamage);
foreach (bool IsEnemy in objectsInsideArea)
{
Debug.Log("You damage the enemy");
}
yield return new WaitForSeconds(2);
}
}
,Hello, im doing my first game and im trying to detect the enemies inside an circle area around my player.
i have two problems right now:
-When a start the game, the circlecollider and player collider is detected as enemies even when i use the compare tag "Enemy"
-My corroutine dont refresh every 2s, and only detect colliders one time when the game start
public class ItemDamage : MonoBehaviour
{
[SerializeField] int damage;
[SerializeField] Collider2D[] objectsInsideArea;
Vector2 radiusOfDamage;
int radius;
public void Start()
{
radiusOfDamage = new Vector2(radius, 0f);
StartCoroutine(DamageEnemy());
}
bool IsEnemy(string tag)
{
for (int i = 0; i < objectsInsideArea.Length; i++)
if (objectsInsideArea[i].gameObject.CompareTag("Enemy"))
{
Debug.Log("object {i} is an Enemy");
return true;
} else
{
Debug.Log("object {i}");
}
return false;
}
IEnumerator DamageEnemy()
{
objectsInsideArea = Physics2D.OverlapAreaAll(Vector2.zero, radiusOfDamage);
foreach (bool IsEnemy in objectsInsideArea)
{
Debug.Log("You damage the enemy");
}
yield return new WaitForSeconds(2);
}
}
Answer by ogulcan_topsakal · Apr 19 at 12:28 PM
@dfmr96 Q: When a start the game, the circlecollider and player collider is detected as enemies even when i use the compare tag "Enemy"
A: Logic is not correct
private bool IsEnemy(Collider2D col)
{
if (col.gameObject.CompareTag("Enemy"))
{
Debug.Log("object {i} is an Enemy");
return true;
}
Debug.Log("object {i}");
return false;
}
Q: My corroutine dont refresh every 2s, and only detect colliders one time when the game start
A: Recall your coroutine after 2 seconds inside coroutine (recursive)
IEnumerator DamageEnemy()
{
objectsInsideArea = Physics2D.OverlapAreaAll(Vector2.zero, radiusOfDamage);
foreach (var col in objectsInsideArea)
{
if (!IsEnemy(col)) continue;
Debug.Log("You damage the enemy");
}
yield return new WaitForSeconds(2);
StartCoroutine(DamageEnemy());
}
as an improvement for the second part i'd suggest to rather choose this solution:
IEnumerator DamageEnemy()
{
var wait2Seconds = new WaitForSeconds(2);
while(true) {
objectsInsideArea = Physics2D.OverlapAreaAll(Vector2.zero, radiusOfDamage);
foreach (var col in objectsInsideArea)
{
if (!IsEnemy(col)) continue;
Debug.Log("You damage the enemy");
}
yield return wait2Seconds;
}
}
This way you have less memory garbage.
My logic was wrong and i forget to add while(true) in the ienumerator to make a loop. Thanks for your answer, now working 100%