Enemy not taking damage for some reason

using UnityEngine;
using RPG.Movement;
using RPG.Core;
using System.Collections.Generic;
using System.Collections;

namespace RPG.Combat
{
    public class Fighter : MonoBehaviour, IAction
    {
        [SerializeField] float weaponRange = 2f;
        [SerializeField] float timeBetweenAttacks = 1f;
        [SerializeField] float weaponDamage = 5f;

        Health target;
        float timeSinceLastAttack = 0;
        private void Update()
        {
            timeSinceLastAttack += Time.deltaTime;

            if (target == null) return;

            if (target.IsDead()) return;

            if (!GetIsInRange())
            {
                GetComponent<Mover>().MoveTo(target.transform.position);
               
         
         
            }
           else
            {
                GetComponent<Mover>().Cancel();
                AttackBehaviour();
            }

        }

        private void AttackBehaviour()
        {
            transform.LookAt(target.transform);
            if (timeSinceLastAttack > timeBetweenAttacks)
            {
                TriggerAttack();
                timeSinceLastAttack = 0;

            }

        }

        private void TriggerAttack()
        {
            GetComponent<Animator>().ResetTrigger("stopAttack");
            GetComponent<Animator>().SetTrigger("attack");
        }

        void Hit()
        {
            if(target == null) { return; }
            target.TakeDamage(weaponDamage);
        }
        private bool GetIsInRange()
        {
            return Vector3.Distance(transform.position, target.transform.position) < weaponRange;
        }
        public bool CanAttack(CombatTarget combatTarget)
        {
            if (combatTarget == null) { return false; }
            Health targetToTest = combatTarget.GetComponent<Health>();
            return targetToTest != null && !targetToTest.IsDead();        
         }
        public void Attack(CombatTarget combatTarget)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            target = combatTarget.GetComponent<Health>();
        }


        public void Cancel()
        {
            GetComponent<Animator>().ResetTrigger("attack");
            GetComponent<Animator>().SetTrigger("stopAttack");
            target = null;
        }
        
    }
}

and the health script

using UnityEngine;

namespace RPG.Combat
{
    public class Health : MonoBehaviour
    {
      [SerializeField] float healthPoints = 100f;

        bool isDead = false;

        public bool IsDead()
        {
            return isDead;
        }


       public void TakeDamage(float damage)
        {
            healthPoints = Mathf.Max(healthPoints - damage, 0);
            if (healthPoints == 0)
            {
                Die();
            }
        }

        private void Die()
        {
            if (isDead) return;

            isDead = true;
            GetComponent<Animator>().SetTrigger("die");
        }
    }
}

i have completed the basic combat lessons but my enemy is not taking damage

So let’s see if we can get to the bottom of this…
First of all, are you getting any errors in your inspector?
Are you using the attack animations we provided or other attack animations?
Do your enemies have Health components?

Let’s add some Debugs to the scripts to make sure things are wired up:
In Fighter.AttackBehaviour()

if(timeSinceLastAttack > timeBetweenAttacks)
{
    Debug.Log($"{gameObject.name} is attacking {target.gameObject.name}");
    TriggerAttack();
    timeSinceLastAttack = 0;
}

In Hit()

void Hit()
{
    if(target==null) return;
    Debug.Log($"{gameObject.name} Hit {target.gameObject.name}");
    target.TakeDamage(weaponRange);
}

In Health.TakeDamage()

       public void TakeDamage(float damage)
        {
            healthPoints = Mathf.Max(healthPoints - damage, 0);
            Debug.Log($"{gameObject.name} has taken {damage} points damage!  OUCH!  {healthPoints} health remaining.");
            if (healthPoints == 0)
            {
                Debug.Log($"{gameObject.name} has been brutally slain in combat!");
                Die();
            }
        }

im not getting errors
i use other mixamo animations i tried 2
yes
and in the inspector im only getting player is attacking enemy

When using animations not made by Explosive, we need to add in the Hit() event into the animation.

Go to the Mixamo animation and click on the fbx file that you imported (within Unity).
The inspector will show tabs for Rig and Animation. Open the Animation window and then scroll down until you find a foldout called Events.
Using the preview tool, move the scrubber in the preview until the animation is where the hit should occur.
In the Event drop down, click Add Event and in the event details box, leave everything alone except the Event name. Make that Hit. Spelling and case matter, it must match the name we use in Fighter (Hit).
Hit apply, and the animation should generate hit events.

well i used this website animations and now it works

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

Privacy & Terms