How to add enemies
Let’s go through a simple example of how to add a new enemy. In this case, our enemy will spawn in the level and perform a dance animation.
Adding a new enemy requires a 3D model, animations and basic to intermediate-level programming skills. The 3D model and animation are already ready and are in the folder Assets/StaticAssets/Tutorial/...
Tutorial Folder Overview
Resources for the tutorial can be found in the Discord channel (fag): Discord
Make sure the animation type is set to Humanoid, the model and animations share the same Avatar.
Assets/StaticAssets/Tutorial/Model/EnemyDancingGraphics


Check the settings of the Dancing animation.
Assets/StaticAssets/Tutorial/Animation/Dancing

Check the Animator component in the EnemyDancingContext prefab.
Assets/StaticAssets/Tutorial/Model/EnemyDancingGraphics

Scripting the Enemy
In the folder
Assets/Scripts/Runtime/Infrastructure/StateMachines, create a script namedEnemyDancingStateMachine.Open the
EnemyDancingStateMachinescript.Ensure that it is placed within its own unique namespace.
Confirm that it inherits from
StateMachine.
EnemyDancingStateMachine class remains empty.
In the folder
Assets/Scripts/Runtime/Installers/, create a script namedEnemyDancingInstaller.
Open the
EnemyDancingInstallerscript.Ensure that it is placed within its own unique namespace.
Confirm that it inherits from
MonoInstaller.Implement the abstract method
InstallBindingsfromMonoInstaller. Leave the method body empty for now.
Create a folder named
EnemyDancingStateMachineinAssets/Scripts/Runtime/Infrastructure/States.
Inside the folder, create a state script named EnemyDancingState.
Within the EnemyDancingState class, inject EnemyDancingStateMachine to enable transitions to other states.
In the
EnemyDancingInstallerclass, inside theBindEnemyStateMachine()method, bind the created state.
In the folder
Assets/Scripts/Runtime/Logic/, create a folder named EnemyDancing, and inside it, create a script called EnemyDancing which will serve as the entry point to the state machine.
Open the EnemyDancingContext prefab.
Assets/StaticAssets/Tutorial/Prefab/EnemyDancingContext
Add two components to it: EnemyDancingInstaller and EnemyDancing.
In the GameObjectContext component, add the MonoInstaller EnemyDancingInstaller.

Factory and Pool
The enemy object will be taken from an object pool. A factory will be used to create enemy instances.
In the GameplayAssetsConfig configuration, create a field enemyDancingPrefab to register the EnemyDancingContext prefab, as well as a field initialEnemyDancingPoolCount to define the initial pool size.
Assets/Scripts/Runtime/Configs/GameplayAssetsConfig
Register the prefab in the config.
Assets/StaticAssets/Configs/GameplayAssetsConfig

Assets/Scripts/Runtime/Infrastructure/Factory/GameFactory/...In the IGameFactory interface, add the factory method contract that states:Any class implementing IGameFactory must be able to create an object of type EnemyDancing.

Implement the method in the
GameFactoryclass.

Assets/Scripts/Runtime/Infrastructure/Factory/...In the generic factoryObjectFactory<T>, implement the call to the factory method to create and return an instance of EnemyDancing.

In the
GameplayBootstrapStateclass, implement the methodCreateEnemyDancingContainer(), which at level start will create the "EnemyDancing" container and populate it with objects.Assets/Scripts/Runtime/Infrastructure/States/GameplayStateMachine/GameplayBootstrapState.cs


EnemyDancing class must implement the IPoolReturnable interface to enable interaction with the object pool.

In the Enums file located at
Assets/Scripts/Runtime/Infrastructure/Enums.cs, add the constant EnemyDancing to the EnemyType enum.

Add functionality in the EnemyPlacer class to access the pool.
Assets/Scripts/Runtime/Logic/Placers/EnemyPlacers.cs

Open the LevelTest prefab and in the Level component, set the Enemy type field to Enemy Dancing.
Assets/StaticAssets/Prefabs/Levels/LevelTest

Make sure that in the LevelList config, only the LevelTest level is set.

Results: Dancing enemies spawn at the specified location in the specified quantity. To implement a wide range of enemy behaviors, you need to create additional states (implementing IState).

Last updated