My character is not taking damage

my health script is corret however my enemy is not taking damage. Any suggestions

Let’s see both your Health.cs and Fighter.cs script, and we’ll take a look.

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");

    }

}

}

using UnityEngine;

using RPG.Movement;

using RPG.Core;

using System;

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();

        }

    }

    internal void Attack(GameObject player)

    {

        throw new NotImplementedException();

    }

    private void AttackBehaviour()

    {

        if (timeSinceLastAttack > timeBetweenAttacks)

        {

            transform.LookAt(target.transform);

            // This will trigger the Hit() event.

            TriggerAttack();

            timeSinceLastAttack = 0;

            //target.TakeDamage(weaponDamage);

           

        }

    }

    private void TriggerAttack()

    {

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

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

        target.TakeDamage(weaponDamage);

    }

    // Animation Event

    void Hit()

    {

        if(target ==null) {return;}

        target.TakeDamage(weaponDamage);

    }

    private bool GetIsInRange()

    {

        return Vector3.Distance(transform.position, target.transform.position) < weaponRange;

    }

    public bool CanAttack(GameObject 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()

    {

        StopAttack();

        target = null;

    }

    private void StopAttack()

    {

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

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

    }

}

}

I moved forward a bit in the lessons but now, Im having another issue that I think is caused by this one.

So far the code looks ok…
Let’s add a few debugs to check what’s going on…
In Fighter.cs, in the Hit() method, add this between the check for a Health target and the damage

Debug.Log($"{name} is hitting {target} for {weaponDamage} points);

And in Health.TakeDamage, add this after the damage calculation:

Debug.Log($"{name} has taken {damage} damage, {healthPoints} points remaining.");

Privacy & Terms