← Back to all addons

Make Enemies Fire Multiple Abilities

Posted by 1MGD on Aug 27, 2024 at 1:47 AM

Feature2.0 Verified
🔥 5 💛 1 1 🥳 1

Hello friends I noticed while creating some more complex enemies that they only fire the first ability in their list. In order to make them fire randomly from the full list I modified the TryToAttackTarget method. Simply highlight the method in your code, control V the below, and hit save. The full file is attached.

private void TryToAttackTarget(float distanceToTarget)
{
    if (m_attackCooldownTimer == 0.0f && distanceToTarget < m_attackTriggerRadius)
    {
        List<ITriggerableAbility> triggerableAbilities = new List<ITriggerableAbility>();

        // Collect all triggerable abilities
        foreach (AbilityBase ability in m_character.abilityInstances)
        {
            if (ability is ITriggerableAbility)
            {
                triggerableAbilities.Add((ITriggerableAbility)ability);
            }
        }

        // Randomly select one of the triggerable abilities
        if (triggerableAbilities.Count > 0)
        {
            int randomIndex = UnityEngine.Random.Range(0, triggerableAbilities.Count);
            m_character.FireAbility(triggerableAbilities[randomIndex]);
            m_attackCooldownTimer = m_attackCooldown;
        }
    }
}

📜 Scripts

💬 Comments (9)

Gyvr Aug 27, 2024 at 03:09 PM
Same comment as <#1277830093041041551> , code blocks would be awesome 😄
1
Zuko Sep 04, 2025 at 06:11 PM
the 300 line code is already updated with the fix or i need to update the method with the first part of the code you provided?
Gyvr Sep 04, 2025 at 06:12 PM
This add-on is still relevant to this day as the AIController hasn’t been modified
Gyvr Sep 04, 2025 at 06:12 PM
Actually…
Gyvr Sep 04, 2025 at 06:12 PM
It has been lol.
Gyvr Sep 04, 2025 at 06:13 PM
Didn’t realize this add-on was from 2024. It should work with M2D 2.x
Gyvr Sep 04, 2025 at 06:13 PM
You might need to adapt it slightly to make it work with 3.x
Zuko Sep 04, 2025 at 06:13 PM
good to know, i will avoit it for now
Zuko Sep 04, 2025 at 06:14 PM
can you give me a helping hand on the support section?
🫡 1

Want to continue the conversation?