Creating a simple enemy in Unity!

Now that we have created our player and a basic combat system, we still need one more essential thing - and that is some obstacle, something to make the player’s life more complicated. And today, we will be creating some simple enemies for that reason.

Setting up Enemy Prefab

First thing first, we need to create a GameObject of our Enemy - for that, we will be using Cube, as we did with the Player. But we will have to differentiate our Enemy somehow from our Player - and for that, we will create a new script called ‘Enemy’ and a new ‘Enemy_mat’ material with the color of your choice, which we will assign both to our Enemy.

Here is your new “best friend”- the Enemy cube. Now take it and place it in your ‘Prefabs’ folder so that we can spawn more of them later on.

Setting up movement

First, as we did in the Player script, we will create a serialized variable _speed. Next, we have to remember the boundaries of our scene on the x and y-axis - for me, that is 11/-11f on the x-axis and -4f/6f on the y-axis.

Next up, we will give our enemy some movement. We will be creating a simple script in which the enemy will move down in a straight line until he reaches the bottom and disappears behind the scene’s edge. For that, we will be using the transform.Translate method, and we will be calling this inside our Update loop.

If our enemy reaches the bottom without getting destroyed(which we still haven’t implemented), instead of destroying him - we will re-use him and place him right at the top of the scene. For that, we will use the Random.Range method to place him in a random spot on the x-axis.

And there we have a basic behavior for our enemy!

The process to create the complete behavior, as you can see in the first gif, will be written down more thoroughly in the upcoming articles - stay tuned!

But that is it for now, thank you for reading and feel free to follow me for more articles — and as always, good luck and see you next time!

Aspiring and eager beginner Unity developer