plusHow 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): Discordarrow-up-right

  1. Make sure the animation type is set to Humanoid, the model and animations share the same Avatar. Assets/StaticAssets/Tutorial/Model/EnemyDancingGraphics

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

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

Scripting the Enemy

  1. In the folder Assets/Scripts/Runtime/Infrastructure/StateMachines, create a script named EnemyDancingStateMachine.

    • Open the EnemyDancingStateMachine script.

    • Ensure that it is placed within its own unique namespace.

    • Confirm that it inherits from StateMachine.

EnemyDancingStateMachine class remains empty.

  1. In the folder Assets/Scripts/Runtime/Installers/, create a script named EnemyDancingInstaller.

  • Open the EnemyDancingInstaller script.

  • Ensure that it is placed within its own unique namespace.

  • Confirm that it inherits from MonoInstaller.

  • Implement the abstract method InstallBindings from MonoInstaller. Leave the method body empty for now.

  1. Create a folder named EnemyDancingStateMachine in Assets/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.

  1. In the EnemyDancingInstaller class, inside the BindEnemyStateMachine() method, bind the created state.

  1. 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.

  1. 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.

  1. 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

  2. Register the prefab in the config. Assets/StaticAssets/Configs/GameplayAssetsConfig

  1. 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.

  1. Implement the method in the GameFactory class.

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

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

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

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

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

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

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

  1. 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