So I have this enemy that spawns two minions. The minions are supossed to follow spawn positions of the Enemy (where the minions got spawned). This works fine but if a instantiate another Enemy the minions follow the enemy that got spawned first.
Im using the A* Pathfinding Project AI for this. Link:(https://arongranberg.com/astar/)
The issue is that im finding the target by the Tag but I dont know how to do it in another way, thats why im here.
Sorry for my bad english btw.
This is the script for one of the minions :
public class MinionPathfinding1 : VersionedMonoBehaviour
{
/// The object that the AI should move to
public Transform target;
public GameObject summoner;
private GameObject Player;
IAstarAI ai;
private void Start()
{
summoner = GameObject.FindGameObjectWithTag("MinionSpawner02");
Player = GameObject.FindGameObjectWithTag("Player");
target = summoner.transform;
}
void OnEnable()
{
ai = GetComponent();
// Update the destination right before searching for a path as well.
// This is enough in theory, but this script will also update the destination every
// frame as the destination is used for debugging and may be used for other things by other
// scripts as well. So it makes sense that it is up to date every frame.
if (ai != null) ai.onSearchPath += Update;
}
void OnDisable()
{
if (ai != null) ai.onSearchPath -= Update;
}
/// Updates the AI's destination every frame
void Update()
{
if (target != null && ai != null) ai.destination = target.position;
if (summoner == null)
{
target = Player.transform;
}
}
}
}
↧