Enemy stops moving and shooting when one of them dies

Updated on November 19, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on November 19, 2017

spawner script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spawn1 : MonoBehaviour {

    public Transform[] enemies;
    public Vector3 spawnValues;
    public float spawnWait;
    public float spawnWaitMost;
    public float spawnWaitLeast;
    public int startWait;
    public bool stop;
    int randenemy;
    // Use this for initialization
    void Start()
    {
        StartCoroutine(waitspawner());
    }

    // Update is called once per frame
    void Update()
    {
        spawnWait = Random.Range(spawnWaitLeast, spawnWaitMost);
    }
    IEnumerator waitspawner()
    {
        yield return new WaitForSeconds(startWait);
        while (!stop)
        {
            randenemy = Random.Range(0, 0);
            Vector3 spawnpos = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 0, Random.Range(-spawnValues.z, spawnValues.z));
            Instantiate(enemies[randenemy], spawnpos + transform.TransformPoint(100, 0, 100), transform.transform.rotation);
            Destroy(enemies[0], 30.0f);
            yield return new WaitForSeconds(spawnWait);
        }
    }
}

health script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class health : MonoBehaviour
{
    [SerializeField] public float Health = 100f;
    private bool Healthp = true;
    public Animator anima;
    
   
    public GameObject enemy;
 
    void Start()
    {
        anima = GetComponent<Animator>();
       
    }
    private void Update()
    {
        
    }
    public void Applydamage(float damage)
    {
        if (Health >= 0f)
            if (Health > 0)
            {
                Health -= damage;
                
            }
            else
            {

                Health = 0f;
                anima.SetBool(“isDead”, true);
                enemyaiscript.isplayeralive = false;
                Destroy(gameObject, t: 3);
             
             
            }
    }

    

}

enemy ai script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.AI;
using UnityEngine.UI;

public class enemyaiscript : MonoBehaviour {
    public Transform player;
    public float playerdistance;
    public float rotationdamping;
    public float movespeed;
    public float damage = 1;
    public static bool isplayeralive = true;
    public Animator anima;
    private Rigidbody rbody;
    private Vector3 moveDir;
    public float wallCheckDist = 0f;
    public float moveForce = 0f;
    Quaternion rotation;
    public LayerMask whatIsObstacle;
    public AudioSource ad;
    public AudioClip ac;
    public ParticleSystem muzzle;
    
 
    
   
    
    // Use this for initialization
    void Start() {
        anima = GetComponent<Animator>();
        rbody = GetComponent<Rigidbody>();
        moveDir = ChooseDirection();
        
      
        
    }

    // Update is called once per frame
    void Update()
    {
        
   
        if (isplayeralive == true)
        {
            playerdistance = Vector3.Distance(player.position, transform.position);
   
                    if (playerdistance <= 1000f)
                    {
                          Lookatplayer();
                        if (playerdistance > 5f)
                        {
                            chase();
                            anima.SetBool(“isRunning”, true);
                            anima.SetBool(“isIdle”, false);
                            anima.SetBool(“isFiring”, false);
                        }

                        else if (playerdistance < 12f)
                        {
                            attack();
                            anima.SetBool(“isFiring”, true);
                            anima.SetBool(“isRunning”, false);
                            anima.SetBool(“isIdle”, false);
                        }
                    }
                
                else
                {
                    anima.SetBool(“isIdle”, true);
                    anima.SetBool(“isFiring”, false);
                    anima.SetBool(“isRunning”, false);

                }
            }
        }
        void Lookatplayer()
        {
        Vector3 direction = player.position – this.transform.position;
        direction.y = 0;

        rotation = Quaternion.LookRotation(player.position – transform.position);

        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationdamping);

        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * rotationdamping);

    }
        void chase()
        {
            transform.Translate(Vector3.forward * movespeed * Time.deltaTime);
        
        

        }
        void attack()
        {
        if (isplayeralive == true)
        {

            RaycastHit hit;
            if (Physics.SphereCast(transform.position,1.0f, transform.forward, out hit))
            {

                if (hit.collider.gameObject.tag == “Player”)
                {
                    dam(hit);
                }
                if (hit.collider.gameObject.layer == 11)
                    Move();
      
            }

        
        }
        }

        void dam(RaycastHit hit)
        {
   
        hit.collider.gameObject.GetComponent<healthcontroller>().Applydamage(damage) ;
            ad.PlayOneShot(ac);
            muzzle.Play();
        
        
    }

        Vector3 ChooseDirection()
        {
            System.Random ran = new System.Random();
            int i = ran.Next(0, 3);
            Vector3 temp = new Vector3();

            if (i == 0)
            {
                temp = transform.forward;
            }
            else if (i == 1)
            {
                temp = -transform.forward;
            }
            else if (i == 2)
            {
                temp = transform.right;
            }
            else if (i == 3)
            {
                temp = -transform.right;
            }

            return temp;

        }
    void Move()
    {
        rbody.velocity = moveDir * moveForce;

        if (Physics.Raycast(transform.position, transform.forward, wallCheckDist, whatIsObstacle))
        {
            moveDir = ChooseDirection();
            transform.rotation = Quaternion.LookRotation(moveDir);
        }

    }
    Vector3 ChooseDirectionLR()
    {
        System.Random ran = new System.Random();
        int i = ran.Next(0, 1);
        Vector3 temp = new Vector3();

        if (i == 0)
        {
            temp = transform.right;
        }
        else if (i == 1)
        {
            temp = -transform.right;
        }

        return temp;

    }
}

please help me asap!!

  • Liked by
Reply