- Home /
Idle gameobjects within spawned trigger wont set trigger off
I have a trigger that is always inactive until certain moments in gameeplay. During these moments it will be enabled. When it's enabled it sets it's position to the player and then makes a list of all game objects with "enemy" tags within it's boundary. My problem is if an enemy is idle OnTriggerEnter and OnTriggerStay won't detect the non moving enemy. If the enemy moves it will detect it without a problem though. I need them to be detected either way. Does anyone know of a solution? - Thanks
Answer by BilboStabbins · Feb 17, 2013 at 11:03 AM
One that springs to mind is to either use a simple distance calculation, say using Vector3.Distance (which doesn't rely on movement) but just checks if an Enemy is within a set distance from the Player.
You could search for every enemy in the scene in your Awake/Start function (using GameObject.FindGameObjectsWithTag) which creates a list of the objects (you could assign this to one called EnemyList). Then, elsewhere in your script you could move through each in the list and do the distance calculation (using
foreach (GameObject enemy in EnemyList)
{
if (Vector3.Distance(EnemyList[enemy].transform.position, Player.position <= myRadius)
// Do Something
{