I am aware of (Physics Overlap) but honestly I do not understand it, hours of looking and research I just cant find something that im looking for… so if anyone could explain this to me please! Greatly appreciated! All I want is when this command is called that any game objects with (“enemy”) tag will be affected or any objects with that tag in distance etc. Here is my code, in C##.
if (animation["footkick"].enabled == true) {
float distance = Vector3.Distance(target.transform.position, transform.position);
if(distance < 8) {
stinkbreathAttack();
}
}
Well you could use FindGameObjectsWithTag() to locate all of the enemies.
if (animation["footkick"].enabled == true) {
GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
foreach(GameObject target in enemies) {
float distance = Vector3.Distance(target.transform.position, transform.position);
if(distance < 8) {
// perform attack on target
}
}
}
While #KellyThomas’s idea would work; it is not ideal. The method FindObjectsWithTag is a heavy method and is not performant. A more efficient way to accomplish this would be this:
if (animation["footkick"].enabled == true)
{
Collider[] enemies = Physics.OverlapSphere(transform.position, RANGE);
foreach (Collider col in enemies)
{
if (col.gameObject.tag == "enemy")
{
Enemy enemy = col.GetComponent<Enemy>();
if (enemy != null)
. {
// Inflict damage
}
}
}
}