I am making a Tower Defense game- so I have multiple enemies- but for some reason when both enemies are inside the radius (Different enemy objects, so like a slow and a fast one, not two of the same like a slow and a slow one), the tower shoots 2 bullets at both enemies instead of targetting one.
Does anyone know how to fix this?
Are there any other events that fire bullets?
Nope, it only has button, firebullet, and Ysort.
Do you have 2 towers in the same spot?
No, and that wouldn’t work anyways since they are all coded to shoot towards the same enemy
Then it’ll be because the events you screen snipped will activate continually until the enemy is no longer collides with Tower_Range, combined with the bullet cooldown period.
is there any way i can make it so it only shoots one bullet at one enemy instead of having it attack 2 at the same time?
First of all, the OR condition doesn’t make sense here because you only have one thing under it.
I don’t see anything that would prevent the towers from shooting at both enemies. The events you are showing do the following:
Enemy1 in range? Shoot at Enemy1
Enemy2 in range? Shoot at Enemy2
In order to prevent both events from triggering, you would need some other condition in there, such as a cooldown for the tower.
I can see another problem which is that you’re not picking the tower objects. So if you have multiple towers then I think they would all shoot, as long as the enemy is colliding with any Tower_Range.
In any case I think a better approach would be to add all Enemy objects to an object group, then create a single event to handle targetting:
repeat for each instance of tower
tower.cooldown = 0 | fire at EnemyGroup
EnemyGroup is within tower.range pixels of tower | tower.cooldown = tower.cooldownMax
pick the EnemyGroup that is closest to tower |
...
tower.cooldown > 0 | tower.cooldown -= 1
This is very helpful, except I don’t understand the
EnemyGroup is within tower.range pixels of tower
part or how to implement it.
There are a couple of ways:
- Increase the fire bullet cooldown timer.
- Add a boolean variable to each tower initially set to false. Add a condition to the events that checks whether this boolean variable is false. When the bullet is fired from the tower, set the boolean variable to true. When there is no enemy colliding with the Tower_range object, reset the boolean variable to false.
As @magicsofa is alluding to, you should really be designing the events to cater for multiple towers and multiple tower types.
It is a built in condition “Distance between objects”
Thank you, but I have one last question.
How do I adjust the cooldown of the tower?
When I change the cooldown from anything other than zero in the behaviors, it stops working.
I tried changing the numbers in the code but that didn’t work either.
What do you mean? I already stated that changing the cooldown in behaviors does not work.
I was able to fix it! Thank you guys.
What I meant by that is that the cooldown behaviour does work. You had an issue somewhere else.
What was the solution? It’s be good to know in case anyone else has the same issue.