Damaging a Custom Character Controller

Important: If you haven't already done so, it is recommended that you set up your player with a EmeraldGeneralTargetBridge first to ensure everything works correctly. If you haven't already done this see the Setting up a Player with Emerald AI section.

Important: It is expected that you have C# coding knowledge before proceeding.

All damage is handled through an interface script called IDamageable. This script acts like a bridge that allows damage calls to be received by any script that uses the IDamageable interface. This is important for custom character controllers as users can then receive the damage call to then damage their character controller's health.

Note: If your player has an EmeraldGeneralTargetBridge script on it, remove it and assign the custom EmeraldPlayerBridge script you create instead.

Copying the EmeraldPlayerBridge Script

To start off, create a copy of the included EmeraldPlayerBridge and rename it to the desired name. This will be used to damage your custom character controller. There are notes where you should add your custom code within the EmeraldPlayerBridge script.

When a player is damaged, it will call the Damage function within this script as shown below. This will call an additional function called DamageCharacterController.

public void Damage(int DamageAmount, Transform AttackerTransform = null, int RagdollForce = 100, bool CriticalHit = false)
{
    DamageCharacterController(DamageAmount, AttackerTransform);

    //Creates damage text on the target's position, if enabled.
    if (CombatTextSystem.Instance != null) CombatTextSystem.Instance.CreateCombatText(DamageAmount, DamagePosition(), CriticalHit, false, false);
}

The DamageCharacterController is where you should put all your character controller's damage code and what happens when your player dies. Because this varies based on character controller, an example can't be provided.

void DamageCharacterController(int DamageAmount, Transform Target)
{
    if (Immortal) return;

    //The code for damaging your character controller should go here.

    OnTakeDamage.Invoke();

    //You should set the Health variables equal to that of your character controller after it was damaged here.

    if (Health <= 0)
    {
        //Controls what happens when your player dies.

        if (m_Collider != null) m_Collider.enabled = false;
        OnDeath.Invoke();
    }
}

Deriving from the EmeraldPlayerBridge script

All relevant functions within the EmeraldPlayerBridge script are virtual and allow you to override them to add your own code.

Below is an example of a script derived from the EmeraldPlayerBridge. At the top are the two most relevant functions; the Start (which should be used for initializing your health values) and the DamageCharacterController (which should be used for damaging your character controller and handling what happens when its health reaches 0).

The other functions can be used to set your character controller's actions such as dodging, blocking, Triggering, given they are supported. By default, these are all set to false.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EmeraldAI;

public class TestPlayerBridge : EmeraldPlayerBridge
{
    public override void Start()
    {
        //You should set the StartHealth and Health variables equal to that of your character controller here.
    }

    public override void DamageCharacterController(int DamageAmount, Transform Target)
    {
        //The code for damaging your character controller should go here.
    }

    public override bool IsAttacking()
    {
        //Used for detecting when this target is attacking.
        return false;
    }

    public override bool IsBlocking()
    {
        //Used for detecting when this target is blocking.
        return false;
    }

    public override bool IsDodging()
    {
        //Used for detecting when this target is dodging.
        return false;
    }

    public override void TriggerStun(float StunLength)
    {
        //Custom trigger mechanics can go here, but are not required
    }
}

Last updated