Enemy Attacking player health not decreasing

When the enemy attacks the player health is not decreasing just displays as the default max value which is settled like 100
even hardcoding and passing numbers like stateMachine.Weapon.SetAttack(20); health just prints 100!
I have attached the health script to my player prefab, in another case the player effectively can decrease the health of the enemy the problem turns out to be I’m invincible now.

Am I missing something else?

my weapon logic:

the first log is from the enemy attacking

Enemy Attacking State script


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyAttackingState : EnemyBaseState
{
    private readonly int AttackHash = Animator.StringToHash("Attack");

    private const float TransitionDuration = 0.1f;

    public EnemyAttackingState(EnemyStateMachine stateMachine) : base(stateMachine) { }

    public override void Enter()
    {
        stateMachine.Weapon.SetAttack(20);

        stateMachine.Animator.CrossFadeInFixedTime(AttackHash, TransitionDuration);
    }

    public override void Tick(float deltaTime)
    {

    }

    public override void Exit() { }
}

Can you post your WeaponDamage.cs and Health.cs? The AttackingState just drives the animation. The actual damage happens when the trigger occurs in WeaponDamage and the Health script is notified.

EDIT: I was referencing the player WeaponLogic in my Enemy state machine script :man_facepalming: :man_facepalming: :man_facepalming:

Hi @Brian_Trotter thank you for the reply
seems like the Damage is always receiving 0 hence when the enemy attacks just displays the max health but I’m not really sure what is causing the issue.

for example clicking the button attack set 30 Damage
image

while the enemy even attacking just stays at 0

image

here are the two scripts:

WeaponDamage.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponDamage : MonoBehaviour
{
    [SerializeField] private Collider myCollider;

    private int damage;

    private List<Collider> alreadyCollidedWith = new List<Collider>();

    private void OnEnable()
    {
        alreadyCollidedWith.Clear();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other == myCollider) { return; }

        if (alreadyCollidedWith.Contains(other)) { return; }

        alreadyCollidedWith.Add(other);

        if (other.TryGetComponent<Health>(out Health health))
        {
            health.DealDamage(damage);
        }
    }

    public void SetAttack(int damage)
    {
        this.damage = damage;
    }
}

Health

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Health : MonoBehaviour
{
    [SerializeField] private int maxHealth = 100;

    private int health;

    private void Start()
    {
        health = maxHealth;
    }

    public void DealDamage(int damage)
    {
        if (health == 0) { return; }

        health = Mathf.Max(health - damage, 0);

        Debug.Log(health);
    }
}

That will do it. I’m glad you were able to get this sorted.

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

Privacy & Terms