Rpg course lecture 37 - enemy using the player animation bug

hi there,
I have this very annoying bug. when I try to click on the enemy , the enemy is giving the punch instead of the player that just stands there. I followed the lecture instruction, and its still giving me this annoying behavior.
if I put only the old enemy everything is ok, but the old enemy only have the health.cs and the combattaget.cs and the capsule collider attached to it. I think it is in the fighter.cs because both pleyer and enemy have a fighter.cs but I still can’t figure it out and I’m not sure it something in the scripts at all. anyone?

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


namespace RPG.combat
{

    public class Fighter : MonoBehaviour, Iaction
    {

        #region fields

        Transform target;

        [SerializeField] float tragetrange = 2f;
        [SerializeField] float timeBtweenAtack = 1f;
        [SerializeField] float weaponDemage = 5f;

        float timeSinceLastAttack = 0;



        #endregion

        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {
            timeSinceLastAttack += Time.deltaTime;
            if(target == null)
            {
                
                return;
            }
            if (!GetsIsInRange())
            {
                GetComponent<Mover>().Moveto(target.position);

                print("moving to target");

            }
            else

            {
                    GetComponent<Mover>().Cancel();
                    AttackBehavior();
                                                      
                                                                                   
               
            }


        }

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

        public void Attack(CombatTarget combatTarget)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            target = combatTarget.transform;
            print("I hit you!");
            


        }
        public void Cancel()
        {

            target = null;

        }
        //this is an animation method
        void AttackBehavior()
        {


            if (timeSinceLastAttack > timeBtweenAtack)
            {
                // this will trigger the hit animation event
                print("triger animation");
                GetComponent<Animator>().SetTrigger("Attack");
                timeSinceLastAttack = 0;

            }
        }

                                                
                
        void Hit()
        {
            print("entering hit method");
            if(target != null)
            {
                Health healthComponent = target.gameObject.GetComponent<Health>();
                healthComponent.TakeDamage(weaponDemage);

            }
        

        }
    }




}

O.k I solved it.

stupid mistake.

it is in the Playercontrol.cs file.

"FindObjectoftype<fighter>.attack in the playercontrol.cs

it should be Getcomponent<fighter>.attack();

because now there are two fighter scripts in the scene.

Good job working this out! FindObjectOfType will always find the first incidence of the class in the heriarchy, where GetComponent will always look on the same GameObject that the script is attached to.

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

Privacy & Terms