Character only hit once after this lesson

so i know at the start, it is said that you might be having a bug with the animation/animator but what exactly is that bug?
since i don’t know i have no idea if it corrolates to the issue i am having, for some reason after doing this lesson my character hits an enemy once and stops, he only will hit that enemy again after walking away pretty far and return afterwards, i reviewed the code, and don’t see a problem with it so is this the bug and should i keep going or is this a different issue entirely?

It’s far back enough, that I don’t remember exactly what the bug being referred to was. Let’s pretend that was never mentioned and think of some ideas that could solve the issue:

stopAttack or attack could be stuck. This can cause the attack to not happen. Adjust TriggerAttack() to read:

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

it did not work, also i didn’t hade private void TriggerAttack because my set trigger was in void cancel like in the video, but even then after i made one it didn’t work either

Paste in your Fighter.cs script and we’ll take a look.

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

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

        private void AttackBehaviour()
        {
            if (timeSinceLastAttack > timeBetweenAttacks)
            {
                //this will trigger the Hit() event
                GetComponent<Animator>().SetTrigger("attack");
                timeSinceLastAttack = 0;
            }
        }

        //animation hit  
        void Hit()
        {       
            target.TakeDamage(weaponDamage);
        }

        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()
        {
            GetComponent<Animator>().SetTrigger("stopAttack");
            target = null;
        }
    }
}

Try this:

        private void AttackBehaviour()
        {
            if (timeSinceLastAttack > timeBetweenAttacks)
            {
                //this will trigger the Hit() event
                GetComponent<Animator>().ResetTrigger("attack");
                GetComponent<Animator>().SetTrigger("attack");
                timeSinceLastAttack = 0;
            }
        }

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

Privacy & Terms