Search Unity

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