Setting up Custom Damage with Emerald AI - Black-Horizon-Studios/Emerald-AI GitHub Wiki

Setting up Custom Damage with Emerald AI

Adding support for damaging custom character controllers or health systems it entirely possible with Emerald AI. However, this requires some basic programming knowledge as users need to know where and how to add custom damage calls through custom functions.

This is broken down into two sections.

Note: If you cannot see the Emerald AI scripts, you will need to add the EmeraldAI namespace at the top of your scripts.

using EmeraldAI;

Damaging an AI

In order for a custom character controller to damage Emerald AI agents, you will need to add some custom code as Emerald AI cannot just automatically detect custom code and functions.

Where the character controller’s weapon script gets a reference to the AI hit, you need to call the Emerald AI Damage function:

The Damage function damages an AI with a customizable damage amount, Target Type, Transform to assign as target, and the amount of ragdoll force applied on death. You would pass your own variables within these paramters, such as the DamageAmount for your player's current weapon and the AttackerTransform for the player's current transform.

Damage (int DamageAmount, TargetType AttackersTargetType, Transform YourTargetsTransform, int RagdollForce)

An example of this could look something like the following. It is important that you use EmeraldAI.EmeraldAISystem.TargetType.Player so the AI know that the target they are attacking is a player. Not doing so can cause detection issues.

Note: YourTargetReference is referring to the target from a collision or raycast that comes from something like a player's weapon.

//Damages an AI to the YourTargetReference object
if (YourTargetReference.GetComponent<EmeraldAI.EmeraldAISystem>() != null)
{
   YourTargetReference.GetComponent<EmeraldAI.EmeraldAISystem>().Damage(YourPlayerDamageAmount, EmeraldAI.EmeraldAISystem.TargetType.Player, YourPlayerTransform, 400);
}

Damaging the Player

All player damage goes through a helper script called EmeraldAIPlayerDamage. Within the script, there is a function called SendPlayerDamage that receives incoming damage from Emerald AI agents (this script is automatically added to your player). You just need to create your own function within this script that damages the character controller player's health (similarly like what is done with the commented out code that damages other character controllers within this script) then call it within the SendPlayerDamage function.

By default, the EmeraldAIPlayerDamage script calls the DamagePlayerStandard function within the SendPlayerDamage. There's also several examples of other character controller integrations so the process can be further shown. These can either be removed or ignored.

Note: Ensure that the CombatTextSystem.Instance.CreateCombatText line is kept as this is what allows the Combat Text System to automatically work with custom character controllers.

Users with custom character controllers will need to create their own function that is called within the SendPlayerDamage function of the EmeraldAIPlayerDamage script that damages their custom character controller. This needs to damage your character controller (in whatever method is proper for your character controller) and monitor the health so you know when the player is dead.

This could look something like this (but of course using your character controller's script and variables):

void DamagePlayerCustom(int DamageAmount)
{
   if (GetComponent<YourPlayerHealth>() != null)
   {
      YourPlayerHealth PlayerHealth = GetComponent<YourPlayerHealth>();
      PlayerHealth.DamagePlayer(DamageAmount);

      if (PlayerHealth.CurrentHealth <= 0)
      {
         //Player is dead
         IsDead = true;
      }
   }
}

You would then need to ensure you call DamagePlayerCustom within the SendPlayerDamage function.

public void SendPlayerDamage(int DamageAmount, Transform Target, EmeraldAISystem EmeraldComponent, bool CriticalHit = false)
{
   //Example code for damaging the DamagePlayerCustom function created above.
   DamagePlayerCustom(DamageAmount);

   //Creates damage text on the player's position, if enabled.
   CombatTextSystem.Instance.CreateCombatText(DamageAmount, transform.position, CriticalHit, false, true);
}