[HELP] Enemy that occupies more than one square


#1

Hello again! I’ve a problem. I’ve a 2 square tall enemy idk if I can post a pic of it (It’s Mussolini upside down walking on his hands :slight_smile: ). He is a mini boss that if defeated it gives you 1 upgrade coin (you could use it to upgrade the defenders). He doesn’t spawn randomly but I’ve set it far from the field so you have 10-15 seconds to prepare your troops. My problem is that he occupies 2 squares but he should be a children of the Spawner of his lane but the problem is that only 1 lane shoots at him and the other one doesn’t do anything. I know the problem it’s in the Projectiles.cs script (it’s a script from the course)

bool IsAttackerAheadInLane()
{
     if (myLaneSpawner.transform.childCount <= 0)
     {
         return false;
     }
     foreach(Transform attackers in myLaneSpawner.transform)
     {
         if (attackers.transform.position.x > transform.position.x)
         {
             return true;
         }
     }

     return false;
 }

maybe I should use a new condition that says If (enemy.transform.position.y==transform.position.y) or something like that. Maybe it’s better use that than all the code

bool IsAttackerAheadInLane()
{
    // ...
}

but it doesn’t work. How should I change that?


#2

Hey Francesco,

Is the boss actually supposed to be occupying two lanes, e.g. the character is supposed to be two lanes wide or, is it just a case that the sprite is tall and effectively overlaps the lane above, because of its height?

Screenshot of the situation during run time?


#3
D                         
D                  _O/
D->  -->  -->  -->  /\

Interesting problem. This might be a good time to teach yourself about raycasting.


#4

he is supposed to be 2 squares tall


when you add 2 gnomes, only one shoots (like Anthony said)

this is not a problem of the box collider 2d (it’s 2 squares tall like him).
Yesterday I made an animation for the Attack state, he should rotate 90 degrees and attack both of the lanes like this attack%20how%20it%20should%20be
but he attacks only 1 lane (top one) mux%20attack
mux%20attack2
then he attacks the other lane.
Now I’m reading how the raycast works on the unity manual hoping that this will work. Thanks for both your answers :slight_smile:


#5

On a related note…you may also want to consider what happens if the boss is spawned in the top lane, as then the other half of it will be all over your UI. Or, bottom lane depending where the over-hang of this sprite effectively is.


#6

Mussolini goes under the defenders select and he goes on top of the level name and the time left bar. For now Mussolini have a pre set spawn (he goes from the spawn to the game view in 20 seconds). If I’ll make a random spawn for him, I’ll copy the spawner script and past it to the 3 middle lanes (the second, the third and the fourth) in the mini boss level.