Search Unity

Multiple Enemies execute animation at same time

Discussion in 'Scripting' started by JMumford45, Nov 12, 2020.

  1. JMumford45

    JMumford45

    Joined:
    Jun 3, 2019
    Posts:
    14
    Hi, still new to development. The problem: when my player collides with one of multiple enemies every enemy starts the attack animation. How to I get only one of the enemies to execute the attack animation??

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Zombie : EnemyBehaviour
    7. {
    8.     protected override void takeDamage(int damage)
    9.     {
    10.         //play hurt sound
    11.         //play animation if available
    12.         //if the sound is playing don't play again till over otherwise play sound
    13.         if(!sound[0].isPlaying)
    14.             sound[0].Play();
    15.         //call base.takeDamage to decrement health
    16.         base.takeDamage(damage);
    17.     }
    18.  
    19.     void OnTriggerEnter2D(Collider2D collision)
    20.     {
    21.         Debug.Log(collision.gameObject.GetComponent<Collider2D>());
    22.         if (collision.gameObject.tag.Equals("Player"))
    23.         {
    24.             isAttacking = true;
    25.             isMoving = false;
    26.             Debug.Log(animate.GetBool("isAttacking"));
    27.             Debug.Log(animate.GetBool("isMoving"));
    28.         }
    29.     }
    30.     void OnTriggerExit2D(Collider2D collision)
    31.     {
    32.         if (collision.gameObject.tag.Equals("Player"))
    33.         {
    34.             isAttacking = false;
    35.             isMoving = true;
    36.         }
    37.     }
    38.  
    39.     private void attackPlayer()
    40.     {
    41.             animate.SetBool("isAttacking", true);
    42.             animate.SetBool("isMoving", false);
    43.     }
    44.  
    45.     private void Update()
    46.     {
    47.         if (isAttacking)
    48.         {
    49.             attackPlayer();
    50.         }
    51.         else
    52.         {
    53.             animate.SetBool("isAttacking", false);
    54.             animate.SetBool("isMoving", true);
    55.         }  
    56.     }
    57. }
    58.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor.Animations;
    4. using UnityEngine;
    5.  
    6. //class contains all the needed data and components of Enemy objects
    7. public class EnemyBehaviour : MonoBehaviour
    8. {
    9.     [System.Serializable]
    10.     public class EnemyStats
    11.     {
    12.         public int health = 10;
    13.         public float speed = 1.0f;
    14.         public int atk_damage = 1;
    15.     }
    16.  
    17.     public EnemyStats stats = new EnemyStats();
    18.     //animator component to control animations
    19.    // [HideInInspector]
    20.     public Animator animate;
    21.     //Rigidbody component to control movement
    22.     [HideInInspector]
    23.     public Rigidbody2D rb;
    24.     //audio source array to store enemy sounds
    25.     public AudioSource[] sound;
    26.     //bool for if enemy is attacking somethinh
    27.     public static bool isAttacking;
    28.     //bool for is the enemy is moving or not
    29.     public static bool isMoving;
    30.  
    31.     private void Awake()
    32.     {
    33.         animate = this.gameObject.GetComponent<Animator>();
    34.         //_collider = GetComponent<Collider2D>();
    35.         sound = this.gameObject.GetComponents<AudioSource>();
    36.         rb = this.gameObject.GetComponent<Rigidbody2D>();
    37.     }
    38.  
    39.     //method to decrement the amount of health
    40.     //destroys enemy when health is zero
    41.     protected virtual void takeDamage(int damage)
    42.     {
    43.         //decrement our max health by damage amount
    44.         stats.health -= damage;
    45.         if(stats.health <= 0)
    46.         {
    47.             //play death animation and sound
    48.             animate.Play("Death");
    49.             //call killenemy to destroy this
    50.             GameManager.KillEnemy(this);
    51.         }
    52.     }
    53.  
    54.     private void OnTriggerEnter2D(Collider2D collision)
    55.     {
    56.         if(collision.gameObject.name.Equals("Hero"))
    57.         {
    58.             Physics2D.IgnoreLayerCollision(9, 14, true);
    59.         }
    60.     }
    61. }
    62.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Your problem is that you're using static variables here:
    Code (CSharp):
    1.     //bool for if enemy is attacking somethinh
    2.     public static bool isAttacking;
    3.     //bool for is the enemy is moving or not
    4.     public static bool isMoving;
    Static variables are shared among all the scripts. There is only one copy in your entire game of those variables and all of your enemies are reading/writing them.
     
  3. JMumford45

    JMumford45

    Joined:
    Jun 3, 2019
    Posts:
    14
    thanks for the quick reply. Okay i fixed that but how to do share those bools with other scripts. I have patrol scripts that need that bool to work??