Exemplo n.º 1
0
        void OnTriggerEnter(Collider other)
        {
            var Root = other.transform.root;

            if (other.isTrigger && TriggerInteraction == QueryTriggerInteraction.Ignore)
            {
                return;                                                                                                                             //just collapse when is a collider what we are hitting
            }
            if (!MalbersTools.Layer_in_LayerMask(other.gameObject.layer, HitLayer))
            {
                return;                                                                      //Just hit what is on the HitMask Layer
            }
            if (Root == Owner.transform)
            {
                return;                                                                    //Don't hit yourself;
            }
            if (!AlreadyHitted.Find(item => item == Root))                                 //if the entering collider is not already on the list add it
            {
                AlreadyHitted.Add(Root);
            }

            if (Root == CurrentAnimal)
            {
                return;                                                           //if the animal is the same, do nothing we already done the logic below
            }
            else
            {
                if (CurrentAnimal)
                {
                    AlreadyHitted = new List <Transform>();                        //Clean the colliders if you had a previus animal
                }
                CurrentAnimal = Root;                                              //Is a new Animal that is enetering the Attack Trigger

                var TargetPos = Root.position;

                var mesh = Root.GetComponentInChildren <Renderer>();
                if (mesh != null)
                {
                    TargetPos = mesh.bounds.center;                                         //Get the mesh Bounds Center
                }
                Vector3 direction = (Owner.position - TargetPos).normalized;                //Calculate the direction of the attack

                var interactable = other.transform.GetComponent <IInteractable>();          //Get the Animal on the Other collider

                interactable?.Interact();

                enemy = Root.GetComponentInChildren <IMDamage>();                           //Get the Animal on the Other collider

                if (enemy != null)                                                          //if the other does'nt have the Damagable Interface dont send the Damagable stuff
                {
                    enemy.HitDirection = direction;
                    enemy.Damage();
                    EnemyStatEnter.ModifyStat(Root.GetComponentInChildren <Stats>()); //Affect Stats
                }
                else if (other.attachedRigidbody && PushForce != 0)                   //If the other has a riggid body and it can be pushed
                {
                    other.attachedRigidbody.AddForce(-direction * PushForce, ForceMode.VelocityChange);
                }
            }
        }