Chracter Health depletes to 0 in combat

As soon as I approach a character, my character dies instantly when attacked. one hit seems to kill my player everytime

Is your player’s health value in the Health component perhaps set very low (in the inspector?).

Is the same thing happening to the enemies when you hit them? (You can test this by disabling the Fighter component on the enemy, giving you a chance to hit them).

My enemy’s health goes down the appropriate way. It’s only when the enemy attack and the AI controller is added that I experience the issue.

even with the fighter disabled my character still dies when I approach the enemy

So… something is hitting the Player even when nothing is attacking?

Paste in your Fighter.cs and Health.cs scripts and we’ll start from there.

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

Fighter Script

using UnityEngine;
using RPG.Movement;
using RPG.Core;

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

        Health target;

        float timeSinceLastAttack = Mathf.Infinity;
        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)
            {
                // This will trigger the Hit() event.
                TriggerAttack();
                timeSinceLastAttack = 0;
            }
        }

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

        // 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(GameObject combatTarget)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            target = combatTarget.GetComponent<Health>();
            Hit();
        }

        public void Cancel()
        {
            StopAttack();
            target = null;
        }

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

The only thing I can think of based on the code is that you might have a CombatTarget on the Player itself, since the only source of damage is the Hit() method…

I want to try something: Change your Hit() method to read like this:

        void Hit()
        {
            if(target == null) { return; }
            if(target.gameObject == this.gameObject)
            {
                  Debug.Log($"{name} is attempting to attack himself!");
                  target=null;
                  return; 
            }
            target.TakeDamage(weaponDamage);
        }

this is what I get as an error:

Hmmm… the warning is indicating that there is a script that got lost somehow but there’s a GameObject with that script attached to it… Go through each of the GameObjects in your scene and look for a Component Missing in the inspector.

The EdgeWakeUp message is a window message… it’s saying there’s something wrong in the current layout. Usually it’s a window that the script no longer exists for. Try resetting the layout.

Neither of these issue should be causing the mysterious Player death issue.
Did you get any of the Debugs saying the Player was trying to hit itself?

Currently, I am not getting any errors. However, my hit is not working when I reset to the lecture setup and remove the HIt()

Without the Hit() event in Fighter, the Hit animation event will not work.

So no debug saying I’m hitting myself

And you’re still dying instantly when you attack the enemy??

Privacy & Terms