- Home /
Better way to check for enemies on the left/right side of "array"
Hi, I am making a space shooter similar to space invaders. Currently, the enemies are parented to a single game object, which moves with a script called EnemySpawn.js. At the moment , the script checks for enemies that are still alive that are located to the farthest left and for enemies that are on the farthest right of the array. If the enemies on either side pass the screen boundaries, the entire game object "bounces" back.
Currently, I have the script set to check for only the top row of enemies that are on the farthest right/or left. This is because enemies in the top row are usually the last remaining enemies after all the enemies below them have been killed. However, in rare cases the top enemy gets killed before the one below it, and the script incorrectly checks if the next enemy in the top row will pass the boundary, causing the enemy in the bottom to temporarily dissapear out of view once the top enemy reaches the boundary.
what is the most efficient way of doing the check so that enemies in the farthest left or farthest right correctly bounce the entire enemy array around, without causing enemy on the farthest side to temporarily dissapear?
Answer by Joyrider · Aug 13, 2013 at 11:09 PM
I doubt it is the most efficient But one idea could be to use a 1 line int array holding the number of enemies per column.
[3][3][3][3][3][3][3]
.X .X .X .X .X .X .X
.X .X .X .X .X .X .X
.X .X .X .X .X .X .X
Everytime an enemy dies, the dying enemy substracts 1 from his column's enemy count.
[3][3][3][3][3][3][2]
.X .X .X .X .X .X .X
.X .X .X .X .X .X .X
.X .X .X .X .X .X
and so on until you get the situation you're talking about
[1][0][2][0][2][3][0]
.. .. .X .. .X .X ..
.X .. .X .. .X .X ..
.. .. .. .. .. .X ..
And this way you still just have to check on line ;)
L0932, if this answer helped you, mark it as the correct answer ( the checkmark below the thumbs). +1 from me for the ASCII pictures (the solution is elegant too...) =)
I have solved my problem, but I will set your response as the correct answer because it's the most informative. If I had more reputation I would also up vote it
Your answer
Follow this Question
Related Questions
The referenced script on this behavior is missing! 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
How would I go About making a flying player move forward, but also Around another object? 0 Answers
Scripting errors I don't know how to fix! Can anyone help? 1 Answer
How can I make the Unity Standard Asset plane move in a scripted manner? 1 Answer