Ok first you may notice a bit of a change to the opening. I’ve added a giant enemy at the top of the screen. I wanted to have something that would kick off the fight. So I thought why not have an out of control enemy get destroyed and then the waves come after the player for doing so. The gif you see was just me adding the new enemy but the spawning won’t actually start until I destroy the big ship. See below:
So that out of the way, let’s look at how I implemented the sneaky enemy. Basically I have it so the enemy will spawn (rarely, about a 15% chance). Once it does it comes down the left of the screen, stops, waits 2 seconds and then begins it’s hunt for the player, moving back and forth on the bottom of the screen.
Yes, this is mean. The player can only destroy the ship before it hits the bottom, or get lucky that a heat seeking missile goes for it. I did set the enemy lasers to destroy the ship as well so maybe the player will get lucky with some friendly fire.
So how did I do it? After putting the new enemy object into Unity I set about with the coding. I added a script called EnemyBackFire and attached it to the enemy.
As mentioned I put this code inside of the enemies own script.
A lot to chew on there. Let’s get to it.
So I created a movement down function that just basically calculated it’s decent to the bottom. Once it hits the -5 on the Y I set the book _isMoving to false so it stops and then run the WantAndThenMove Coroutine.
Inside of that I just have the enemy ship wait 2 seconds then start to move left (just so it gets to the left of the screen first) then off to the right! Once it’s one it’s way to the right I call another function that calculates the movement while it’s on the bottom.
That’s just a few basic if statements that while moving right, move at this speed until you hit a certain position. Once there, set moving right to false and begin to move left.
Now — giving it a brain to fire!
So, I didn’t want it just randomly firing at will. I wanted it to search for the player and when it detects the player in front of it, fire. Unity has a perfect thing for that — raycasting!
So, what I’m doing here is making a bool method with a return. Basically I set the initial value to false and then give the float variable castDist the value of distance (which we will hand over later). Then we need an endposition, a Vector2 variable with work for that and I just take the distance position and add the movement up (you want the raycast shooting up in this case and then pass in the endpos. The last part is just telling it what layer it should be looking for. I set my Player layer to Active back in Unity and then call that.
That’s basically it for the raycast. A very good video that explains this far better than I is here:
The last part of the code is passing in the collider effect that will hit the “Player” tag.
After all of this I just went into my Spawn Manager script and added this enemy to the list of things to spawn.
So I’m trying to decide what to add now — either a powerup collector (when the player presses the C button and is in range of a powerup it will magnetize it and shoot it into the player. Or, giving the enemy the power to shoot a powerup if it detects it.
Check in tomorrow to find out!