When I attack, Health is not reduced

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

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

    Transform target;

    private void Update()
    {
        timeSinceLastAttack += Time.deltaTime;

        if (target == null) return;

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

    private void AttackBehavior()
    {
        if (timeSinceLastAttack > timeBetweenAttacks)
        {
            //This will trigger the Hit() event
            GetComponent<Animator>().SetTrigger("attack");
            timeSinceLastAttack = 0;
        }
    }
    //Animation Event
    void Hit()
    {
        target.GetComponent<Health>().TakeDamage(weaponDamage);
    }

    private bool GetIsInRange()
    {
        return Vector3.Distance(transform.position, target.position) < weaponRange;
    }

    public void Attack(CombatTarget combatTarget)
    {
        GetComponent<ActionScheduler>().StartAction(this);
        target = combatTarget.transform;
    }

    public void Cancel()
    {
        target = null;
    }

}

}

Oddly enough, if I put “TakeDamage(5)” instead of “TakeDamage(weaponDamage)” it works appropriately

It sounds like you’ve set the value of weaponDamage to 0 in the inspector window. That will override the value in your code.

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

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

    public void TakeDamage(float damage)
    {
        health = Mathf.Max(health - damage, 0);
        print(health);
    }
}

}

WOW! Thats it. Not sure how that happened. Lol. Thanks

1 Like

No problem. I promise it won’t be the last time it happens! :slight_smile:

1 Like

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

Privacy & Terms