- Home /
Attacking mulitple targets simultanously
Hi Pros,
i am a newbie hobby programmer since 6 month and i love it. I tryed right now a Tower def game with a good tutorial and just wanted to add some extra stuff to train my skills. Right now the towers just attack one enemy if he is dead or out of range they target the next.
I want to add a tower that can attack all enemys. I tried it with an new list in wich i add all enemys that enter the collider, and with a foreach loop i wanted to set them as targets. If i read the logs thos works but my damn tower doenst want to attack simultaneously. He attacks the enemys right one after another. Any tipps would be great and dont be to harsh with me, just learning tight now :D thx for help :)
MonoBehaviour { [SerializeField] private string projectileType;
[SerializeField]
private int damage;
private Monster target;
private bool canAttack = true;
private float attackTime;
List<Monster> attackList = new List<Monster>();
[SerializeField]
private Animator animator;
[SerializeField]
private float attackCooldown;
[SerializeField]
private float projectileSpeed;
public int MyPrice { get; set; }
public float MyProjectileSpeed { get => projectileSpeed; set => projectileSpeed = value; }
public Monster MyTarget { get => target; set => target = value; }
public int MyDamage { get => damage; set => damage = value; }
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
SetTarget();
Attack();
}
public void SetTarget()
{
if(target == null && attackList.Count > 0)
{
foreach (Monster monster in attackList)
{
target = monster;
Debug.Log(monster);
}
}
}
public void Attack()
{
if (target != null && target.IsActive)
{
Shoot();
}
if (target != null && !target.IsAlive || target != null && !target.IsActive)
{
target = null;
}
}
private void Shoot()
{
Projectile projectile = GameManager.MyInstance.MyPool.GetObject(projectileType).GetComponent<Projectile>();
projectile.transform.position = transform.position;
projectile.Initialize(this);
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Monster")
{
attackList.Add(other.GetComponent<Monster>());
Debug.Log(MyTarget);
}
}
public void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Monster")
{
attackList.Remove(other.GetComponent<Monster>());
target = null;
}
}
}
Answer by revolute · Dec 05, 2019 at 01:24 AM
In SetTarget
, this line:
if(target == null && attackList.Count > 0)
prevents you from multi targeting. For the first time that a GameObject is enlisted in the attackList
, SetTarget
is called and target
is set to that object. Until it dies or leaves the trigger, target
will not change and only one will be attacked.
Either make the variable target
to a list or an array to hold multiple targets and handle them in attack or just call Attack
in SetTarget and cleanup the target
variable right after.
Answer by Anis1808 · Dec 05, 2019 at 01:46 AM
Your problem occurs on line 49, specifically target = monster;
because in your foreach loop, you are overriding your target variable EACH time, so the target will only take the last entry of the loop. In your situation (and I assume this script is for the multi-target tower only), I would add a new function in the GameManager script, which I dont have access right now that takes a list as input and that instantiates a single projectile for each of your Monsters and call it on line 73 of the current script. Since all the code for spawning is in the GameManager, I cannot help you with code so I wish my explanation helps you, otherwise please provide me with the GameManager script so I can give you better explanation.
Thx for Help, will look in this solution too. Always good to know more ways:) Thank you so much guys