Sometimes the player attacks, sometimes not

Hello,
I’m not getting any errors either, I did everything sam and rick did. I don’t understand why this is the case. why is the player acting like this :frowning: any suggestions?
Thank you :frowning:

Sorry about the delay.

Paste in your Fighter.cs script and we’ll start from there. One thing to try first, though, is to put

GetComponent<Animator>().ResetTrigger("Attack");

in front of the call to SetTrigger(“Attack”);

Hello again,

I wrote attack in lowercase because that’s how I used it in the trigger’s name. And in last picture include another problem of mine. When the player dies he flies up like this :smiley: Maybe this level is not suitable for me :frowning: Thank you for your help in advance

For future code pastes, please paste in the text of the file, not screenshots…
In AttackBehavior, let’s add a Debug in the if(timeSinceLastAttack > timeBetweenAttacks) block:

Debug.Log($"{gameObject.name} is triggering an attack on {target.gameObject.name}");

It says Player is triggering an attack on Enemy but he punches the air not enemy, so he dies in the end. And sometimes he does not move and freezes for a while.

Paste in the text of your Fighter script (the image is now gone) so I can take a closer look.

namespace RPG.Combat
{
    public class Fighter : MonoBehaviour, IAction, ISaveable
    {

        [SerializeField] float timeBetweenAttacks = 1f; 
        [SerializeField] Transform rightHandTransform = null;
        [SerializeField] Transform leftHandTransform = null;
        [SerializeField] Weapon defaultWeapon = null;

        Health target;
        float timeSinceLastAttack= Mathf.Infinity;
        Weapon currentWeapon = null;


        private void Start()
        {         
            if(currentWeapon == null)
            {
                EquipWeapon(defaultWeapon);
            }
          
        }

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

            if (target == null) return; 

            if (target.IsDead()) return; 

            if (!GetIsInRange())
            {
                GetComponent<Mover>().MoveTo(target.transform.position, 1f);
            }
            else
            {
                GetComponent<Mover>().Cancel();
                AttackBehaviour();
            }

        }

        public void EquipWeapon(Weapon weapon)
        {
            currentWeapon = weapon;
            Animator animator = GetComponent<Animator>();
 
        }


        private void AttackBehaviour()
        {
            transform.LookAt(target.transform);

            if(timeSinceLastAttack> timeBetweenAttacks)
            {
                //This will trigger the Hit
                TriggerAttack();
                timeSinceLastAttack = 0;
                Debug.Log($"{gameObject.name} is triggering an attack on {target.gameObject.name}");
            }

        }

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

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

            if (currentWeapon.HasProjectile())
            {
                currentWeapon.LaunchProjectile(rightHandTransform, leftHandTransform, target);
            }
            else
            {
                target.TakeDamage(currentWeapon.GetDamage());
            }
            
        }

        void Shoot()
        {
            Hit();
        }

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

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

        public void Cancel()
        {
            StopAttack();
            target = null;
            GetComponent<Mover>().Cancel();
        }

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

        public object CaptureState()
        {
            return currentWeapon.name;
        }

        public void RestoreState(object state)
        {
            string weaponName = (string)state;
            Weapon weapon = Resources.Load<Weapon>(weaponName);
            EquipWeapon(weapon);
        }
    }

}

I should also point out that in chapter 43 he told us to write that way:

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

so that was the first version of the method. After you said try to put resetTrigger (“attack”) I changed it that way.

I’ve found that the original methodology of resetting attack before setting stopAttack and vice versa leaves some instances where things aren’t reset properly.
If you call reset on Attack before calling Attack, you’re guaranteed the trigger will fire.

As such, I would also use

GetComponent<Animator>().ResetTrigger("stopAttack");
GetComponent<Animator>().SetTrigger("stopAttack");

when stopping an attack.

When you say he’s punching the air, not the enemy, is this because the enemy moved out of range? This could be that the stopAttack isn’t reset (see above code).

If that still doesnt’ fix it, I’m not seeing anything in the code that could be causing it (something could be there, I’m just not seeing it). That could mean it’s an issue in the inspector. If the change on stopAttack to ResetTrigger(“stopAttack”) followed by SetTrigger(“stopAttack”) doesn’t do the trick, then I’ll need a closer look.

In that case, zip up your project and upload it to https://gdev.tv/projectupload Be sure to remove the Library folder to conserve space…

1 Like

I updated stopattack as you used it and it’s fixed thank you so much, have a nice day.

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

Privacy & Terms