[Solved] Enemies ignore their weapon damage

I just realized that enemies ignore their weapon damage. It works for their level (progression SO). But for weapons that dont work. If i increase the weapon damage that dont work. If i increase range , enemy attack me from further away.
For player fighter script works.

I have Should Use Modifier activated on all enemies prefabs and on all enemies on scene. I really dont understand how it can works for player and not for enemies.

Thanks for helping.

It didn’t occur to me in a previous answer to this: Are you implementing Inventory and removing the IModifierProvider from Fighter? Since the enemies don’t have a StatsEquipment with an item in the Weapon Slot, this would cause them not to get their weapon bonuses.

The trick is not doubling the Player’s weapon bonuses if this is the case…

In my own game, I actually put StatsEquipment components on the enemies, and added a field to the StatsEquipment for a weapon to equip automatically.

I did the 3 first courses with the same project.
I have inventory and Quest & Dialogues actually, Starting RPG Shop.

I dont understand how i can fix my issue. I would like to have weapon damage working as before.

Should i add the IModifier provider on fighter script ?

Thnaks for your help.

here is my fighter script :

using UnityEngine;

using RPG.Movement;

using RPG.Core;

using System;

using GameDevTV.Saving;

using RPG.Attributes;

using RPG.Stats;

using System.Collections.Generic;

using GameDevTV.Utils;

using GameDevTV.Inventories;

namespace RPG.Combat

{

    public class Fighter : MonoBehaviour, IAction, ISaveable

    {

        [SerializeField] float timeBetweenAttacks = 1f;

        [SerializeField] Transform rightHandTransform = null;

        [SerializeField] Transform leftHandTransform = null;

        [SerializeField] WeaponConfig defaultweapon = null;

        [SerializeField] float autoAttackRange = 4f;    

        Health target;

        Equipment equipment;

        float timeSinceLastAttack = Mathf.Infinity;

        WeaponConfig currentWeaponConfig;

        LazyValue<Weapon> currentWeapon;

        private void Awake()

        {

            currentWeaponConfig = defaultweapon;  

            currentWeapon = new LazyValue<Weapon>(SetupDefaultWeapon);

            equipment = GetComponent<Equipment>();

            if (equipment)

            {

                equipment.equipmentUpdated += UpdateWeapon;

            }

        }

        private Weapon SetupDefaultWeapon()

        {  

            return AttachWeapon(defaultweapon);

        }

        private void Start()

        {

            currentWeapon.ForceInit();

        }

        private void Update()

        {

            timeSinceLastAttack += Time.deltaTime;

            if (target == null) return;

            //if (target.IsDead()) return;

            if (target.IsDead())

            {

                target = FindNewTargetInRange();

                if (target == null) return;

            }

            if (target != null && !GetIsInRange(target.transform))

            {

                GetComponent<Mover>().MoveTo(target.transform.position, 1f);

            }

            else

            {

                GetComponent<Mover>().Cancel();

                AttackBehaviour();

            }

        }

        public void EquipWeapon(WeaponConfig weapon)

        {

            currentWeaponConfig = weapon;

            currentWeapon.value = AttachWeapon(weapon);

        }

        private void UpdateWeapon()

        {

            var weapon = equipment.GetItemInSlot(EquipLocation.Weapon) as WeaponConfig;

            if (weapon == null)

            {

                EquipWeapon(defaultweapon);

            }

            else

            {

                EquipWeapon(weapon);

            }

        }

        private Weapon AttachWeapon(WeaponConfig weapon)

        {

            Animator animator = GetComponent<Animator>();

            return weapon.Spwan(rightHandTransform, leftHandTransform, animator);

        }

        public Health GetTarget()

        {

            return target;

        }

        private bool GetIsInRange(Transform targetTransform)

        {

            return Vector3.Distance(transform.position, targetTransform.position) < currentWeaponConfig.GetRange();

        }

        public bool CanAttack(GameObject combatTarget)

        {

            if (combatTarget == null) return false;

            if (!GetComponent<Mover>().CanMoveTo(combatTarget.transform.position) && !GetIsInRange(combatTarget.transform))

            {

                return false;

            }

            Health targetToTest = combatTarget.GetComponent<Health>();

            return targetToTest != null && !targetToTest.IsDead();

        }

        public void Attack(GameObject combatTarget)

        {

            GetComponent<ActionScheduler>().StartAction(this);

            target = combatTarget.GetComponent<Health>();

            //print("Take that you short, squat paesant!");

        }

        private void AttackBehaviour()

        {

            transform.LookAt(target.transform);

            if (timeSinceLastAttack > timeBetweenAttacks)

            {

                // This will trigger the Hit() event

                TriggerAttack();

                timeSinceLastAttack = 0;

            }

        }

        private Health FindNewTargetInRange()

        {

            Health best = null;

            float bestDistance = Mathf.Infinity;

            foreach (var candidate in FindAllTargetsInRange())

            {

                float candidateDistance = Vector3.Distance(

                    transform.position, candidate.transform.position);

                if (candidateDistance < bestDistance)

                {

                    best = candidate;

                    bestDistance = candidateDistance;

                }

            }

            return best;

        }

        private IEnumerable<Health> FindAllTargetsInRange()

        {

            RaycastHit[] raycastHits = Physics.SphereCastAll(transform.position,

                                                autoAttackRange, Vector3.up);

            foreach (var hit in raycastHits)

            {

                Health health = hit.transform.GetComponent<Health>();

                if (health == null) continue;

                if (health.IsDead()) continue;

                if (health.gameObject == gameObject) continue;

                yield return health;

            }

        }

        private void TriggerAttack()

        {

            GetComponent<Animator>().ResetTrigger("stopAttack");

            GetComponent<Animator>().SetTrigger("attack");

        }

       

        // Animation Event

        void Hit()

        {

            if (target == null) return;

            float damage = GetComponent<BaseStats>().GetStat(Stat.Damage);

            if (currentWeapon.value != null)

            {

                currentWeapon.value.OnHit();

            }

            if (currentWeaponConfig.HasProjectile())

            {

                currentWeaponConfig.LaunchProjectile(rightHandTransform, leftHandTransform, target, gameObject, damage);

            }

            else

            {

                target.TakeDamage(gameObject, damage);

            }            

        }

        //Animation Event

        void Shoot()

        {

            Hit();  

        }

        public void Cancel()

        {

            StopAttack();

            target = null;

            GetComponent<Mover>().Cancel();

        }

        private void StopAttack()

        {

            GetComponent<Animator>().ResetTrigger("attack");

            GetComponent<Animator>().SetTrigger("stopAttack");

        }

        public object CaptureState()

        {

            return currentWeaponConfig.name;

        }

        public void RestoreState(object state)

        {

            string weaponName = (string)state;

            WeaponConfig weapon = Resources.Load<WeaponConfig>(weaponName);

            EquipWeapon(weapon);

        }        

    }

}

Thanks Brian, you solved my problem.

I added IModifierProvider on my fighter script. Now it works.

Maybe i did a copy paste when after a chapter, my code did not work correctly.
I forgot to add the provider after the copy paste.

I hope i not did that for other scripts . I was thinking the code was the same.
I will check all the scripts. I see i miss FindNewTargetInRange and FindAllTargetInRange too.

public IEnumerable<float> GetAdditiveModifiers(Stat stat)

        {

            if (stat == Stat.Damage)

            {

                yield return currentWeaponConfig.GetDamage();

            }

        }

        public IEnumerable<float> GetPercentageModifiers(Stat stat)

        {

            if (stat == Stat.Damage)

            {

                yield return currentWeaponConfig.GetPercentageBonus();

            }

        }

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms