- Home /
Find Objects with Tag in Distance (AOE Spell)
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();
}
}
Answer by KellyThomas · Jan 12, 2014 at 06:30 AM
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
}
}
}
When object with tag is destroyed, the script glitches
Are you perfor$$anonymous$$g the foreach iteration over a changing collection? It won't like that!
But by using enemies as a stable intermediary it shouldn't be a problem.
What specifically is going wrong?
The programme plays different music when in battle. When distance is higher than limit (not battle) - the not battle music plays (correctly). When distance is lower than limit (battle) - the battle music plays. When enemy is destroyed (from battle to not battle) the music doesn't play (wrong, not battle music should play). I will try to change loops
Answer by Reid_Taylor · Apr 15, 2020 at 06:15 PM
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
}
}
}
}
@Reid_Taylor Could you please explain the above?
I am trying to implement this to a land trigger, looking for adjacent land triggers around it within a specific range, then opening the closed lands around it, and generating new closed lands around the new open land..
public class LandTriggerScript : MonoBehaviour
{
public bool isLandOpen = false;
public bool hasNorthLand = false;
public bool hasEastLand = false;
public bool hasSouthhLand = false;
public bool hasWestLand = false;
public bool hasNorthEastLand = false;
public bool hasSoutEastLand = false;
public bool hasSouthWestLand = false;
public bool hasNorthWest = false;
public GameObject[] adjacentTriggers;
public void PrepareTriggers()
{
Collider[] enemies = Physics.OverlapSphere(transform.position, 30);
foreach (Collider col in enemies)
{
if (col.gameObject.tag == "MapTrigger")
{
//add game object to the array of gameobjects named adjacentTriggers
}
}
for(a = 0 ; a < adjacentTriggers.length; a++)
{
//if the gameobject's x axes is smaller than transform's x value, set the transform's hasWestLand boolean true,
//as well as the opposite side for the target game object...and so on for all 8 sides
}
}
}
Your answer
Follow this Question
Related Questions
Distance from tagged objects problem 1 Answer
How is OverlapSphere used? 2 Answers
how can i find the closest object (with the tag Point) 1 Answer
Distance thing with gameobject ? 4 Answers