In this section, you will create a simple enemy that can move around the map.
Kenney provides hundreds of free assets to the public domain that can be used to help in your prototypes (and even commercial projects). In this section, you will use the Graveyard Kit to create an enemy using one of the 5 provided models and animations.
Before starting, you should import the FBX Models from this asset pack: Graveyard Kit ยท Kenney
If you need help importing the models, you can refer to the Importing Asset section of 02 - Creating a Tile Grid > Importing Assets
Here is a quick check list to help you:
Start by creating an Empty Game Object in your Hierarchy that will act as the base for your Enemy.
Note: Double check that the new Game Object is not a child of any other object.
In the Kenney Graveyard Kit you will find 5 character models that can be used as a model for your Enemy:
In the Project tab, if you search for character-
, you should see the 5 models:
You can search your assets for a specific file by using the search bar at the top of the Project explorer tab.
character-zombie
model. However, feel free to use any of the models.Can you remember how to unpack a prefab? Notice, the character-xyz
model you have selected is a Prefab in the Hierarchy. Can you unpack it?
enemy-ufo-
model in your Hierarchymodel
When you have finished, your Hierarchy should look similar to the image below:
Now that you have an Enemy game object, turn it into a Prefab. At this point, you now have multiple types of Prefabs (tiles and enemies). Create a folder for each within your Prefabs folder to help organize them.
When you have finished, your Prefabs folder should look similar to the folders below:
To make your Enemy move, you will need to create a MonoBehaviour. A MonoBehaviour is a scripted component that provides methods for managing the behaviour of your Game Objects while the game is running.
Create a MonoBehaviour script for your enemies movement called EnemyMovement
After the script loads, verify that the class name is EnemyMovement
. If you did not rename the script immediately upon creation in Unity, the template will set the name to be NewMonoBehaviourScript
. It is important to rename this when you open the file for the first time.
When named correctly, it should match the name of the file. For example, in VS Code:
Now that you have a EnemyMovement script, you can attach it to your Enemy Game Object
If all went well, you should now see an Enemy Movement component in the Inspector
You may have noticed that the Enemy Movement component in your inspector has a small green plus icon next to it. This means that the component you added only exists on this instance of your Enemy and not your Enemy Prefab.
Can you remember how to apply Overrides to your Prefab?
When you are done, the green plus icon should no longer be visible on the Enemy Movement component in the inspector.
One of the most useful methods that are provided by a MonoBehaviour is the Update method. This method is called by the Unity Engine each frame your game is running. This can be used to update the Transform Position of a GameObject each frame to make it move in the game world.
Change your EnemyMovement script's Update method to increase the enemy's transform.position on the X axis each frame.
Be sure to save your work. Then, return to the Unity Editor and enter Play Mode. If all went well, you should see your Enemy moving along the x-axis of your game world. You can verify this in the Inspector.
You may have noticed the Enemy is not moving at a consistent rate. This is because, the Enemy Movement scripts Update method is being called each frame. This makes the movement of the Enemy dependent on the Frame Rate of the game.
To help with this, you can scale the movement using the amount of time that has passed between frames. In Unity, this value can be accessed using the Time.deltaTime
property.
Time.deltaTime
Once again, enter Play Mode and verify your Enemy's Transform Position is updating. You will notice your Enemy moves much slower than it did before. This is because it is now moving at 0.1 units per second. You can think of the expression * Time.deltaTime
as "per second". For example, if you want an object to move 5 units per second, you would use 5 * Time.deltaTime
.
Different Enemies will move at different speeds. To help accommodate this, add a Speed
property to your EnemyMovement
script.
float Speed
property to your EnemyMovement
class
Speed
to 1f
Speed
property rather than the float literal 0.1
.
Once again, enter Play Mode and verify that your Enemy moves at 1 unity per second.
It is useful to be able to modify a property from the Unity Inspector. To do this, you must Serialize your property by adding the [field: SerializeField] attribute to the property that you would like to see in the inspector.
[field: SerializeField]
to your Speed propertyNow, when you select your Enemy, you will see the Speed property in the Enemy Movement component in the Inspector. You can adjust this value directly in the Inspector to change the speed of your Enemy. You can even do this while in Play Mode! However, be aware that when you exit Play Mode any changes you made will be reset to their original values.
Using what you've learned in this lesson, create a new MonoBehaviour Script called "EnemyRotation" that allows you to specify how quickly an Enemy rotates on the Y axis.
EnemyRotation
script90f
(e.g. 90 degrees per second)private setter
Time.deltaTime
When you have successfully implemented the EnemyRotation script, it should work similar to the image below:
In the next section you will create a Waypoint component that you can use to specify the path your enemies will take.