Today, we are taking a deeper look into the enemies. How they are built, how they behave and then we will take a look at the scripts.
For this post, I ‘am going to create a new enemy from scratch, while trying to reuse general scripts.
Today I will show you how to create “Ghost”, a completely new enemy that I will create as I write this. Ghost’s ability will be partly invisible. It will do damage on impact, and move to another location and attack again. A dungeon nightmare with other words.
Step 1: Manage the sprites
To begin with, we need a sprite. We have downloaded free sprites from LPC. The first thing we have to do, is slicing the sprite. The sprite is represented as a “.png” file. To slice the sprite, I highlight the sprite, select sprite mode: multiple and press “Sprite editor”. From here we have to do a little try and fail to slice the sprite correctly. Sometimes it slices automatically correct, but sometimes we have to manually try different slice sizes to find the correct one. In this case, I had to slice it manually with pixel size 40×46.
Step 2: Creating animation
After the slicing, I now have 12 different pictures that represent the enemy. I now have to create animations. In total 8 animations. Four moving animations, one In each direction, and four idle animations. We also need an death animation, which is created is the same way, but it can be whatever you like. I will use a green explosion animation.
To do this, I select those pictures I want to use for an animation, right click -> create -> animation, and give it a proper name, e.g. GhostMoveLeft or GhostIdleRight.
Next, I move all of my animations to the animation folder, and create an animator controller called GhostAC.
Here we create 3 new states: idle, isWalking and isDead with transition between them as shows in the picture below. We also need to create some parameters: valueX, valueY, isWalking and isDead. We use isWaling and isDead as conditions for entering/leaving isWaling and isDead animations. After we have created the motion field, it is important to check “Loop time”, so the animation is not just played once.
That’s all for the animations, lets move over to create the physical part of the ghost.
Step 3: Adding the enemy to the game
In my scene I create an empty game object. I start by moving this game object closer to the player, so I can see the enemy when I start the game. But the enemy is not visible yet. First we have to add a sprite render. For the game object, we have an inspector panel to edit it. From here we add the sprite render (which is a component). Now we just have to drag the sprite into the sprite render, and we have a visual enemy.
Step 4: Adding the animator controller
Now we have to add the Ghost animator controller that we created earlier. There is two ways of doing this. You can do it the normal way by finding “Animation” in the component list, or you can do it the easy way. You can just drag “GhostAC” into the inspector panel, and it automatically add the animation controller. Pretty easy.
Step 5: Adding box collider
Next the enemy need a box collider. This is so other objects, as the player, cannot run though the enemy.
Step 6: Adding rigidbody
Then we need to add a rigidbody, which gives the enemy physics in the game such as mass, gravity and collision.
Step 7: Scripting
Now comes the fun part. The first thing we want to add is the enemy stats script which controll the health, damage and stats. The setup for code is pretty simple.
First, you start with field variables and public variables. public variables can be changed in inspector and will be unique for each game object that uses the script. Field variables will be initialized by the constructor name Start()
</span>Animator anim; GameObject statScaler; GameObject player; Scaler level; <div>public int attackDamage; public int health; public bool debug; public int damageIncrease; public int yieldExp; <span style="font-family: Courier New;">