How can I have my bullet hit more than 1 enemy?
I have a bullet attached to my towers and when monster 1 comes in the sphere collider of my tower then it shoots the monster. How would i go about adding a second OnTriggerEnter so my bullet can recognize my second monster?
Adding code in (formatted) regular text is better -- easier to read and easier for someone to paste and test it.
Specific "help me with my game" Qs are also best in the Help Room (I moved this Q there.) You don't have to wait in the $$anonymous$$oderation queue.
Answer by Eno-Khaon · Jun 12, 2016 at 08:21 AM
How do you hit multiple enemies? Just keep track of how many you want to hit. Right now, your limit is simply "1"
// ...
public float speed = 10;
public Transform target;
public int targetLimit = 5; // hits up to 5 enemies;
private int currentTargetCount = 0; // Keeps track of enemies hit
// ...
void OnTriggerEnter(Collider co)
{
// ...
health.decrease();
currentTargetCount++;
if(currentTargetCount >= targetLimit)
{
Destroy(gameObject);
}
// ...
}
It may take a little more fine-tuning, but for each enemy you hit up to your limit, keep going. Dealing with actually reaching the target is another matter, though. If you wish to pass through the target, you could fix the direction of travel to the last known direction after contacting the target. Otherwise, the projectile could be destroyed upon reaching the target:
if(currentTargetCount >= targetLimit || co.transform == target)
{
Destroy(gameObject);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612082751im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player takes damage through collider tag 0 Answers
Im tying to make independent health bars for each entity the code is attached to. 0 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers
Screen space overlay canvas problems. 1 Answer
Fall Damage script not working 1 Answer