Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Stop other enemies!

Discussion in 'Scripting' started by gaishinbu, Jun 26, 2019.

  1. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Let's assume; There are 5 enemies. How can I stop other enemies for 2 seconds if I kill one? I don't wanna stop player so i can't use "Time.timeScale = 0" . I mean I don't know how to use it.

    My enemy script;

    Code (CSharp):
    1.  
    2. public class Enemy : MonoBehaviour
    3. {
    4.     public GameObject player;
    5.     public float movementSpeed = 4;
    6.  
    7.     void Start()
    8.     {
    9.      
    10.         player = GameObject.FindWithTag("Player");
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.        
    16.         transform.LookAt(player.transform);
    17.         transform.position += transform.forward * movementSpeed * Time.deltaTime;
    18.     }
    19.  
    20.     void OnCollisionEnter (Collision col)
    21.     {
    22.         if (col.gameObject.tag.Equals ("Bullet"))
    23.         {
    24.          
    25.             Destroy (col.gameObject);
    26.             Destroy(transform.parent.gameObject);
    27.             Destroy (this.gameObject);        
    28.         }
    29.     }
    30. }
    31.  
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    What do you mean by stop them? Do you want the animations to freeze, or just stop attacking the player? In any case, you'll need the enemies to have a script that can be told when they should pause, and then have them tell all the other enemies to pause when they die. FindObjectOfType<EnemyScript>() could be useful for that.
     
    gaishinbu likes this.
  3. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    The enemies are just walking towards the player. No attacks. When I kill one, I want the others to stay in place for two seconds. Then I want them to walk back to the player.
     
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Oh, make a cooldown timer for them.
    Code (csharp):
    1.  
    2. public class Enemy : MonoBehaviour
    3. {
    4.     public GameObject player;
    5.     public float movementSpeed = 4;
    6.     private float wait = 0; // Added
    7.  
    8.     void Start()
    9.     {
    10.    
    11.         player = GameObject.FindWithTag("Player");
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if(wait > 0)  // Added
    17.         {
    18.            wait -= Time.deltaTime;
    19.            return; // exit update early when our timer is active, so we don't move anymore
    20.         }
    21.         transform.LookAt(player.transform);
    22.         transform.position += transform.forward * movementSpeed * Time.deltaTime;
    23.     }
    24.  
    25.     void OnCollisionEnter (Collision col)
    26.     {
    27.         if (col.gameObject.tag.Equals ("Bullet"))
    28.         {
    29.        
    30.             Destroy (col.gameObject);
    31.             Destroy(transform.parent.gameObject);
    32.             Destroy (this.gameObject);
    33.             foreach(var enemy in FindObjectsOfType<Enemy>()) // Added
    34.                enemy.wait = 2.0f; // this can be whatever time you want
    35.         }
    36.     }
    37. }
     
    Lethn and gaishinbu like this.
  5. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    :eek: Wow! You saved my life! Thank you so much!!!!:D:D:D