- Home /
How to issue order to enemies
I am making a top down shooter game but I cannot get this last mechanic to work. After one enemy has detected the player, if the player remains detected the enemy should notify all other enemies to move towards the player.
I have tried using events and delegates but to no avail. All the enemies use the same script "Enemy Controller" and therefore when I set a boolean in one enemy it only changes on the one enemy. This is the crux of my problem.
Answer by MarshallN · Feb 22, 2017 at 03:25 PM
So you're trying to have all enemies become active and chasing when any enemy sees the player? You could use a public static bool, which only one instance of is stored and all copies of the script reference - example:
public static bool playerSpotted;
void Update()
{
// If player is spotted:
{
playerSpotted = true; // This updates the public static bool playerSpotted, which every copy of this script shares.
}
if(playerSpotted == true) // true if ANY copy of this script has spotted the player.
{
// Move towards the player
}
}
Im not currently at home but if the static bool is the answer, you have just saved me hours of testing and iterating. Thanks @$$anonymous$$arshallN
public static variables aren't a very elegant solution to most problems, but they can be useful at times. You could also have a public static void() function that aggros all enemies, and have individual timers on each script that have them lose interest if they don't see the player.
Another way to do it would be to have a Game$$anonymous$$anager script on an empty GameObject in the scene, and have that script have a public variable that deter$$anonymous$$es whether enemies should be active or not. The enemy script would look at the Game$$anonymous$$anager script to see if that bool is true, and if so they would chase.
So yeah! There are a few ways to do it, play around with it and see what feels right for you!
Your answer
Follow this Question
Related Questions
Send Mouse Event 0 Answers
Unity Mechanim playing default layer state animation when gameObject is duplicated in the scene 0 Answers
How to add external method from class to Unity3dEvents AddLisitener()? 1 Answer
Enemy follow player when player is not looking. OnBecameInvisible not working. 1 Answer