- Home /
Check Between Enemy List
Hi, i'm trying to finish a TD and i have the last problem. When i press return enemies spawn and they get into a List and walk in paths, i have towers near the paths so they can shoot. My problem is that i do a for to recognize all of them and check wich is the nearest to my tower. i have a state pattern in my towers so i have something like this. What i try to do is : i have 2 enemies, and two towers, i want to make the tower attack the nearest enemy, and the aother tower attack the otherone (that is nearest to it) but my problem is that both towers recongnize the first enemy and wait him to be nearest and perhaps another enemy pass behind a tower and it dont attack :(
in State Iddle (Context is the ''ParentClass'')
public void UpdateIdle(Context context) {
context.myEnemyList = context.enemiesList.enemies;
if (context.tower)
{
if (context.myEnemyList != null)
{
for (int i = 0; i < context.myEnemyList.Count; i++)
{
context.myEnemyList[i] = GameObject.FindWithTag("Enemy");
if (Vector3.Distance(context.tower.transform.position, context.myEnemyList[i].transform.position) < 15)
{
context.enemy = context.myEnemyList[i];
Debug.Log(context.i);
context.SetState(new StateAttack());
}
}
}
}
This is kind of a standard problem in Tower Defence games. One popular solution is to mode the towers manually- the player can click on a tower, and tell it what enemy to prioritise (front, back, strongest, weakest, and so on).
Answer by seedoubleyou · Dec 13, 2011 at 01:15 AM
How about a "I'm Being Attacked" boolean? When the tower01 attacks, the enemy01 "I'm Being Attacked" boolean becomes true. So, when tower02 goes to attack enemy01, it finds the boolean switched on, so it ignores it and looks for a different enemy?
Your answer
Follow this Question
Related Questions
Move the enemy object opposite to player and keep its distance relative to the player? 1 Answer
Need help with tower defense win level system,need help with tower defense win level system 0 Answers
Check for an instantiated object farthest from player? 1 Answer
check if an always increasing variable has stop increasing 2 Answers