Adding a Boss to 2D Galaxy Shooter in Unity-Part 02-Boss Movement!

Objective: To implement the final Boss’s movement for our 2D Galaxy Shooter.

Now that we have setup the basics for the Boss prefab asset, let’s jump into the Boss’s movement as shown above.

Create a new script and add it to the Boss prefab, don’t forget to save! Open it up and let’s add some code:

First we need a couple variables for the boss’s speed and a speed multiplier. We also need a boundary marker for when the boss changes directions.

Lastly, we need a couple of trigger switch bools for our initial movement down into the middle of the screen and then the trigger to move left and right.

This is going to be a nice simple movement using the transform.position of the boss. We are going to do an If check to see if _moveRight is true and if it is then we are going to move the boss to the right. Then we need another If check to see if the boss reaches the boundary and when it does, flip the trigger switch to false.

It will now dive into the Else If statement as it is false and it will move the boss to the left boundary where the trigger switch will flip again, creating the constant back and forth.

Firstly we are going to complete an If check to see if _startMovement is true which it will always be when the boss first spawns. This indicates that we want to move the boss from the top of the screen to the middle point before it starts its right to left movement. For this movement I have used Vector3.Lerp as it looked like a nice smooth entrance.

Then we are going to check for when it’s transform position reaches 1.3 of the Y-axis and when that does we are going to flip our trigger switches so that the _startMovement is false and the _moveRight is true.

Next Up: Part 03 — Boss Weapons