Hit detection not syncing with attack speed?

When I click to attack, my animation loops properly and cancels, but it is independent of the actual hit detection.

Instead of taking damage on hit, the target takes damage as soon as “attack” triggers, and then their hp keeps ticking down to 0 as long as the attack is not canceled. So if I set timeBetweenAttacks to 4, the player will punch once, but the target will continuously lose health until it dies.

I am not sure what I did wrong, I made sure my animator settings and Fighter class are the same as the video, but I’m still missing something.

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

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

        private void AttackBehavior()
        {
            transform.LookAt(target.transform);
            if (timeSinceLastAttack > timeBetweenAttacks)
            {
                //triggers the Hit() event. 
                TriggerAttack();
                timeSinceLastAttack = 0;
            }
        }

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

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

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

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

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

        //Animation Event
        void Hit()
        {
            if(target == null) { return; }
            target.TakeDamage(weaponDamage);
        }

        public bool CanAttack(CombatTarget combatTarget)
        {
            if(combatTarget == null) { return false; }
            Health targetToTest = combatTarget.GetComponent<Health>();
            return targetToTest != null && !targetToTest.IsDead();
        }
    }
}

Has anyone had this issue before? I have spent all day looking at this. My animator settings are the same as the video, so is my code, and so are both the player and enemy prefabs.

Edit: It looks like TakeDamage() is being called twice somehow. The target takes damage every time the animation plays, and they take automatically every second as long at the player is attacking.

It was because I had the Character prefab in the scene as well. I didnt notice the lecture didn’t have one in there. Removed it and its working. Weird bug, I wonder if it gets mentioned later in the lecture.

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

Privacy & Terms