Aggressive enemies, pulling powerups, and negative pickup items.

Chance Cornell
3 min readAug 2, 2021

Today’s article will be a little different than in the past. I realized that I have been typing a ton of information that isn’t necessarily needed. I feel that I have been over-explaining things instead of just explaining what the code snippets do. So I will be doing just that. I’ll try posting my code snippets and explain what is happening in that instead of writing everything out then attaching the code pieces.

I also will go over three things I added to the game today that are fairly simple and quick. The first will be adding a negative powerup, which I chose to remove all ammo from the player if they collect the item.

All I did for this was in the Player script add this function which sets the ammo count to zero if the player touches it, and the UI will be updated.

Had to add a case statement to the Powerup script to enable it. Don’t forget to add your prefab to the Spawn Manager game object in Unity.

Now we will create an aggressive enemy that will charge the player when they are too close. For the moment, I just created the logic for the enemy, not the enemy itself that will spawn with other enemy types.

Here we move the enemy down just like the main enemy we created a while back. We’re also running two if statements at the same time, one checking for when the player hits the bottom of the screen (the top if statement), and the second, checking for the distance between itself and the player. That statement is taking the distance between the two and checks if it is less than 5. If it is, then it will move towards the players’ position at the speed of 1.5f.

The final thing I did was use the same logic to pull the powerup items to the player if they were in range. I wanted the event to happen while pressing ‘C’, so I wrote it with input in mind. The following snippet is in the Update function of the Powerup script.

I hope this article was a quicker and easier to understand read. It definitely took less time for me to write and gives me more time to work on the project. I was spending almost an hour per article trying to write it out how I was, and today was much quicker. Thanks for reading.

--

--