# Implementing Galaga Style Enemy Behavior in Unity

I've been trying to work on a space shooter with the idea of having enemies behaving like it is shown in this video : https://www.youtube.com/watch?v=3p7u8uCR6yw

In the video above, enemies fly from different portions outside the view area of the screen doing elaborate movement before finding a spot to form an organized formation.

I've been thinking of ways how this could all be done. One way is by having either the enemies and/or a manager use a Finite State Machine that will control when there is a "performing" state and when there is a "FindingSpot" state.

but at as far as implementing the complex patterns, I'm at a loss. Is there some sort of Math formulation that's used to generate those movement patterns?

If I understand, your question is about the building of the formation (arrival of the enemies from outside the screen into a "space-invaders like" formation (aka end-formation)).

Having played this game a lot, here is how I think they have done it:

• They have a handful of "incoming trajectories" which come from out of the screen to an end-point roughly in the middle of the screen, just under the end-formation.
• Those trajectories are likely to be pre-computed, and stored as lists of x,y coordinates (regarding the age of the game, they probably have been drawn on paper and "digitized" by hand).
• Each enemy has a "terminal" position in the end-formation (some of them don't; that's the ones that will try to dive on you)
• All enemies move following the chosen trajectory until the end-point, and then they continue to their assigned position in the end-formation more or less in straight line

To make it smoother, you may give each enemy a different exit point for the trajectories (that would be the index of the last point of the trajectory before going to its end-position). It may make the transition more natural. Also the diving enemies will leave the trajectory a lot earlier, and probably re-use the algorithm that guide missiles to the player ship.

Also, the end-formation is slowly moving horizontally. When the enemy will go in straight line from the end-point toward its end-position, you need to go to the position where the end-point will be at the time of arrival (aim in advance); or you may try to aim the current position of the end point; it could make the trajectory more natural, but you'll have to try to be sure.

Bottom-line: This method was common in the 80s. On today's hardware, you can probably model the trajectories as mathematical functions; that would allow you to have a smother movement, and less pre-computed data; also you may use some polynomial function instead of "straight line" for the last part of the arrival. The rest of the algorithm doesn't change (go to trajectory end-point, then go to your end-position)

EDIT: I forgot the last part of your question: I'm not sure there is a "simple" mathematical function for those trajectories, but you may obtain something close by stitching ellipses arcs together.

Here is an attempt to visualize it:

Here the trajectory is decomposed in 3 ellipse arcs (first the black, then the red and the green. The "Pinkish" part is the last part of the arrival (the straight line).