- Home /
other enemies attack when they see one of them be killed by the player
Hello, I try to make all enemies convert to an attackMode (a boolean) when one is killed. For that I need my enemy script to know when an other enemy is killed close enough (under a rayCast number). Do you have any idea on how I can do that ? Thank you :)
Answer by GrayLightGames · Oct 30, 2019 at 04:20 AM
One option would be to make an enemy manager script. In the script, you can have a list of all the enemies. So when an enemy dies, it calls a function in the manager script with the death position as an argument to check all the enemies to see if they are close enough and then alert the ones that are. Just make sure if you are spawning enemies to add them to the list on spawn and remove them on death, after the alert processing of course. Hope that helps!
Answer by Reedex · Oct 31, 2019 at 10:14 AM
Second option without the use of manager would be that Enemy.cs (or whatever you call your actor script) would have Die() Method in it it would call Physics2D.OverlapCircleAll for 2d or Physics.OverlapSphere for 3d. Get all those enemies in range of that overlap radius(use layermask so it doesn't care for not-enemy-colliders), loop through those that were found, and set those booleans.
This is cleaner than my solution, so while a manager script might be useful for something else, I'd recommend managing the alerting like this either way. I believe it would be OverlapCircleAll and OverlapSphereAll for a list... is that right @Reedex?
If you ask about syntax,yes 2d is "OverlapCircleAll" yet 3d is just "OverlapSphere". If you ask about what it returns its an array. (i edited the original answer so the syntax fits) : -)
Your answer
Follow this Question
Related Questions
Astar Pathfinding with unity 2d 1 Answer
"the object of type transform has been destroyed but you are still trying to access it" problem, 1 Answer
How to implement proper enemy follow and shoot 0 Answers
How to make unit priority targeting system? 2 Answers
How to stop enemy AI from vibrating when tracking y component 0 Answers