Example #1
0
        // Update is called once per frame
        void Update()
        {
            //Called as first thing, so if it is dead we don't waste cpu
            ani.SetFloat("moveSpeed", agent.velocity.magnitude);
            ani.SetInteger("hp", currentHP);

            if (currentHP <= 0)
            {
                deathTimeCounter += Time.deltaTime;
                if (acid)
                {
                    Instantiate(deathCloud, transform.position, Quaternion.identity);
                    acid = false;
                }
                if (deathTimeCounter > deathDelay)
                {
                    ScoreManager.Increase(1);

                    var pc = Player.GetComponent <Player.PlayerController>();

                    int health = 0;
                    int ammo   = 0;


                    //This is to prevent any null references
                    if (pc != null)
                    {
                        health = pc.playerHealth.currentHealth;
                        ammo   = pc.WeaponController.GetAmmo();
                    }
                    if (HealthDropPrefab == null)
                    {
                        HealthDropPrefab = GameObject.FindWithTag("Health");
                    }
                    if (AmmoDropPrefab == null)
                    {
                        AmmoDropPrefab = GameObject.FindWithTag("Ammo");
                    }



                    //Drop health
                    if (health < ammo)
                    {
                        //var healthObj = GameObject.FindWithTag("Health");
                        var h = Instantiate(HealthDropPrefab, agent.transform.position, agent.transform.rotation) as GameObject;
                        h.transform.position = agent.transform.position;
                    }
                    //drop ammo
                    else if (health > ammo)
                    {
                        //var ammoObj = GameObject.FindWithTag("Ammo");
                        var h = Instantiate(AmmoDropPrefab, agent.transform.position, agent.transform.rotation) as GameObject;
                        h.transform.position = agent.transform.position;
                    }
                    //drop health
                    else
                    {
                        //var healthObj = GameObject.FindWithTag("Health");
                        var h = Instantiate(HealthDropPrefab, agent.transform.position, agent.transform.rotation) as GameObject;
                        h.transform.position = agent.transform.position;
                    }
                    pc.WeaponController.WeaponExp += 100;
                    Destroy(this.gameObject);
                }

                return;
            }

            //transform.rotation = Quaternion.Euler(-90.0f, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);

            attackTime += Time.deltaTime;
            if (attackReleased)
            {
                attackAnimateTime += Time.deltaTime;
                if (attackAnimateTime > attackSpeed)
                {
                    attackAnimateTime = 0;
                    attackReleased    = false;
                    ani.SetBool("isAttacking", false);
                    if (Muzzle)
                    {
                        var bullet = Instantiate(Projectile, Muzzle.transform.position, Muzzle.transform.rotation) as GameObject;
                        bullet.GetComponent <Rigidbody>().velocity = bullet.transform.TransformDirection(Vector3.forward * 100);
                    }
                }
            }
            if (Vector3.Distance(transform.position, Player.transform.position) <= maxPursueDistance)
            {
                transform.LookAt(Player.transform);
                //  transform.LookAt(new Vector3(Player.transform.position.x, 1.0f, Player.transform.position.z));

                if (isRanged && attackTime > attackSpeed)
                {
                    attackTime = 0;
                    ani.SetBool("isAttacking", true);
                    attackReleased = true;
                }
                //This is so the enemy doesn't dry hump the player
                if (Vector3.Distance(transform.position, Player.transform.position) > minPursueDistance)
                {
                    Vector3 m = new Vector3(1f, 1f, 1f);
                    m = transform.forward * MoveSpeed * Time.deltaTime;

                    transform.position += m;
                    ani.SetBool("isAttacking", true);
                    attackReleased = true;
                }
                else if (!isRanged)
                {
                    ani.SetBool("isAttack", true);
                    attackReleased = true;
                }
            }
            else
            {
                timer += Time.deltaTime;
                //ani.SetBool("isAttacking", false);
                if (timer >= wanderTimer)
                {
                    var newLocation = wander.WanderLocation(transform.position, wanderRadius);
                    agent.SetDestination(newLocation);
                    transform.LookAt(newLocation);

                    timer = 0;
                }
            }


            if (time > 0f)
            {
                time -= Time.deltaTime;
            }
            else
            {
                dischrg.Stop();
            }
            if (charge > 0f)
            {
                Collider[] hitColliders = Physics.OverlapSphere(transform.position, charge);
                if (hitColliders.Length != 1)
                {
                    GameObject target = this.gameObject;
                    float      close  = charge * 10 * charge * 10;

                    for (int i = 1; i < hitColliders.Length; i++)
                    {
                        float distance = (transform.position - hitColliders[i].transform.position).sqrMagnitude;

                        if (distance < close)
                        {
                            close = distance;
                            switch (hitColliders[i].transform.tag)
                            {
                            case "Enemy":
                                target = hitColliders[i].gameObject;
                                break;

                            default:
                                break;
                            }
                        }
                    }

                    if (target != this.gameObject)
                    {
                        Discharge(target);
                    }
                }

                charge -= Time.deltaTime;
                //print("CHARGE:" + charge);
            }
            else
            {
                //print("No Charge");
                charge = 0f;
                acid   = false;
            }
        }