Beispiel #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (!dead)
        {
            if (!activated)
            {
                if (Vector3.Distance(gameObject.transform.position, playermovement.target) < activationdistance)
                {
                    activated = true;
                    //gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = true;
                }
            }
            else
            {
                bulletcount++;
                if (bulletcount > bulletinterval)
                {
                    gun.Shoot();
                    bulletcount = 0;
                }
                Vector3 direction = gameObject.transform.position - player.transform.position;
                direction.Normalize();
                gameObject.transform.up = Vector3.RotateTowards(gameObject.transform.up, direction, 0.1f, 0.1f);


                speed = speed / 3;
                speed = new Vector3(speed.x - (gameObject.transform.up.x * movementspeed), speed.y - (gameObject.transform.up.y * movementspeed), speed.z - (gameObject.transform.up.z * movementspeed));

                gun.gameObject.transform.LookAt(player.transform.position);
                if (twodee)
                {
                    gameObject.transform.position = new Vector3(0, gameObject.transform.position.y + speed.y, gameObject.transform.position.z + speed.z);
                }
                else
                {
                    gameObject.transform.position = new Vector3(gameObject.transform.position.x + speed.x, gameObject.transform.position.y + speed.y, gameObject.transform.position.z + speed.z);
                }
            }
        }
    }
    void FixedUpdate()
    {
        if (!dead)
        {
            if (!activated)
            {
                if (Vector3.Distance(gameObject.transform.position, playermovement.target) < activationdistance)
                {
                    activated = true;
                    //gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = true;
                    navvy.speed    = walkingspeed;
                    animator.speed = walkingspeed / 3.5f;
                    animator.SetBool("standing", false);
                    if (walkingspeed > 9)
                    {
                        animator.SetBool("fastwalk", true);
                    }
                    else
                    {
                        animator.SetBool("forwards", true);
                    }


                    this.gameObject.GetComponent <UnityEngine.AI.NavMeshAgent>().destination = playermovement.target;
                }
            }
            if (activated)
            {
                if (state == 0)
                {
                    pushtest();
                    if (!attacking)
                    {
                        animator.SetBool("walkleft", false);
                        animator.SetBool("walkright", false);
                        this.gameObject.GetComponent <UnityEngine.AI.NavMeshAgent>().destination = playermovement.target;
                        RaycastHit hit;
                        Vector3    target = new Vector3(playermovement.target.x, playermovement.target.y + 1.15f, playermovement.target.z);

                        if (Physics.Raycast(head.transform.position, target - head.transform.position, out hit, 10000))
                        {
                            if (hit.collider.gameObject.tag == "Player")
                            {
                                attacking = true;
                                animator.SetBool("fastwalk", false);
                                animator.SetBool("forwards", false);
                                //animator.SetBool("standing", true);
                                animator.speed = 1;
                                navvy.speed    = 0;
                                gameObject.GetComponent <enemyik>().aiming = true;
                                activatedcounter = 0;
                            }
                        }
                    }
                    else
                    {
                        if (attackstate != retreating)
                        {
                            if (attackstatecount > changeattackinterval - 1)
                            {
                                attackstatecount     = 0;
                                changeattackinterval = Random.Range(50, 700);
                                float tak = Random.Range(-1, 1);
                                if (tak < -1)
                                {
                                    attackstate = standing;
                                }
                                else
                                {
                                    attackstate = strafe;
                                }
                                if (attackstate == strafe)
                                {
                                    animator.SetBool("forwards", false);
                                    animator.SetBool("fastwalk", false);
                                    animator.SetBool("standing", false);



                                    strafespeed = Random.Range(-0.2f, 0.2f);
                                    if (strafespeed < 0)
                                    {
                                        animator.SetBool("walkright", false);
                                        animator.SetBool("walkleft", true);

                                        animator.speed = -strafespeed * 10;
                                    }
                                    if (strafespeed > 0)
                                    {
                                        animator.SetBool("walkleft", false);
                                        animator.SetBool("walkright", true);

                                        animator.speed = strafespeed * 10;
                                    }
                                    if (strafespeed < 0.01f && strafespeed > -0.01f)
                                    {
                                        animator.SetBool("walkright", false);
                                        animator.SetBool("walkleft", false);
                                        animator.SetBool("standing", true);
                                        animator.speed = 1;
                                        strafespeed    = 0;
                                    }
                                }
                                if (attackstate == standing)
                                {
                                    animator.SetBool("walkright", false);
                                    animator.SetBool("walkleft", false);
                                    animator.SetBool("standing", true);
                                    animator.speed = 1;
                                }
                            }
                            if (attackstate == strafe)
                            {
                                if (strafespeed < 0)
                                {
                                    animator.SetBool("walkright", false);
                                    animator.SetBool("walkleft", true);
                                    RaycastHit hits;

                                    if (Physics.Raycast(head.transform.position, -gameObject.transform.right, out hits, 3))
                                    {
                                        if (hits.distance < 1)
                                        {
                                            strafespeed = -strafespeed;
                                            animator.SetBool("walkleft", false);
                                            animator.SetBool("walkright", true);
                                        }
                                    }
                                    // animator.speed = -strafespeed / 2;
                                }
                                if (strafespeed > 0)
                                {
                                    animator.SetBool("walkleft", false);
                                    animator.SetBool("walkright", true);
                                    RaycastHit hits;
                                    if (Physics.Raycast(head.transform.position, gameObject.transform.right, out hits, 3))
                                    {
                                        if (hits.distance < 1)
                                        {
                                            strafespeed = -strafespeed;
                                            animator.SetBool("walkright", false);
                                            animator.SetBool("walkleft", true);
                                        }
                                    }

                                    // animator.speed = strafespeed / 2;
                                }
                                if (strafespeed == 0)
                                {
                                    animator.SetBool("walkright", false);
                                    animator.SetBool("walkleft", false);
                                    animator.SetBool("standing", true);
                                    animator.speed = 1;
                                }
                                gameObject.transform.LookAt(playermovement.target, Vector3.up);
                                gameObject.transform.Translate(strafespeed / 5, 0, 0);
                            }
                            if (attackstate == standing)
                            {
                                gameObject.transform.LookAt(playermovement.target, Vector3.up);
                            }
                            attackstatecount++;
                            bulletcount++;
                            activatedcounter++;
                            if (bulletcount > bulletinterval)
                            {
                                bulletinterval = Random.Range(30, 200);
                                bulletcount    = 0;
                                gun.Shoot();
                            }

                            if (Vector3.Distance(playermovement.target, gameObject.transform.position) < reatreatabldistance)
                            {
                                retreat();
                            }

                            RaycastHit hit;
                            if (activatedcounter > 50)
                            {
                                Vector3 target = new Vector3(playermovement.target.x, playermovement.target.y + 1.15f, playermovement.target.z);

                                if (Physics.Raycast(head.transform.position, target - head.transform.position, out hit, 10000))
                                {
                                    if (hit.collider.gameObject.tag != "Player")
                                    {
                                        attacking = false;

                                        animator.SetBool("forwards", true);

                                        animator.SetBool("standing", false);
                                        animator.SetBool("walkright", false);
                                        animator.SetBool("walkleft", false);
                                        navvy.speed = walkingspeed;
                                        gameObject.GetComponent <enemyik>().aiming = false;
                                        animator.speed = walkingspeed / 3.5f;
                                    }
                                }
                            }
                        }
                    }
                    if (attackstate == retreating)
                    {
                        attackstatecount++;
                        if (Vector3.Distance(gameObject.transform.position, navvy.destination) < 5 || attackstatecount > 500)
                        {
                            attackstate = strafe;
                            attacking   = false;

                            animator.SetBool("forwards", true);

                            animator.SetBool("standing", false);
                            animator.SetBool("walkright", false);
                            animator.SetBool("walkleft", false);
                            navvy.speed = walkingspeed;
                            gameObject.GetComponent <enemyik>().aiming = false;
                            animator.speed = walkingspeed / 3.5f;
                        }
                    }
                }
                if (state == flamethrower)
                {
                    pushtest();
                    bulletcount++;

                    if (bulletcount > bulletinterval)
                    {
                        bulletinterval = Random.Range(30, 100);

                        bulletcount = 0;
                        gun.Shoot();
                    }
                    if (!attacking)
                    {
                        /*RaycastHit hits1;
                         * if (Physics.Raycast(head.transform.position, -gameObject.transform.forward, out hits1, 20))
                         * {
                         *  if (hits1.distance < 20)
                         *  {
                         *      if(hits1.collider.gameObject.tag=="baddie")
                         *      {
                         *          attacking = true;
                         *          animator.SetBool("forwards", false);
                         *          animator.SetBool("fastwalk", false);
                         *          animator.SetBool("standing", true);
                         *          animator.speed = 1;
                         *          navvy.speed = 0;
                         *      }
                         *
                         *
                         *  }
                         * }*/

                        attackstatecount++;
                        if (attackstatecount > changeattackinterval)
                        {
                            changeattackinterval = Random.Range(500, 1000);
                            attackstatecount     = 0;

                            strafespeed = Random.Range(-3, 3);
                            gameObject.GetComponent <UnityEngine.AI.NavMeshAgent>().destination = playermovement.target + gameObject.transform.right * strafespeed;
                        }
                        if (strafespeed < 0)
                        {
                            RaycastHit hits;
                            if (Physics.Raycast(head.transform.position - gameObject.transform.forward, -gameObject.transform.right, out hits, 10))
                            {
                                if (hits.distance < 1)
                                {
                                    strafespeed = -strafespeed;
                                }
                            }
                            // animator.speed = -strafespeed / 2;
                        }
                        if (strafespeed > 0)
                        {
                            RaycastHit hits;
                            if (Physics.Raycast(head.transform.position - gameObject.transform.forward, gameObject.transform.right, out hits, 10))
                            {
                                if (hits.distance < 1)
                                {
                                    strafespeed = -strafespeed;
                                }
                            }

                            // animator.speed = strafespeed / 2;
                        }

                        //gameObject.transform.Translate(strafespeed/10, 0, 0);
                        if (Vector3.Distance(gameObject.transform.position, navvy.destination) < 3)
                        {
                            navvy.SetDestination(playermovement.target);
                        }
                        else
                        {
                            gameObject.GetComponent <UnityEngine.AI.NavMeshAgent>().destination = playermovement.target + gameObject.transform.right * strafespeed;
                        }


                        if (Vector3.Distance(gameObject.transform.position, playermovement.target) < 3 && !attacking)
                        {
                            RaycastHit hit;
                            if (Physics.Raycast(head.transform.position, head.transform.forward, out hit, 400))
                            {
                                if (hit.collider.gameObject.tag == "Player")
                                {
                                    attackstatecount = 100000;
                                    attacking        = true;
                                    animator.SetBool("forwards", false);
                                    animator.SetBool("fastwalk", false);
                                    animator.SetBool("standing", true);
                                    animator.speed = 1;
                                    navvy.speed    = 0;
                                }
                            }
                        }
                    }
                    if (attacking)
                    {
                        gameObject.transform.LookAt(playermovement.target);
                        //strafecontrol();
                        RaycastHit hit;
                        if (Physics.Raycast(head.transform.position, head.transform.forward, out hit, 400))
                        {
                            if (hit.collider.gameObject.tag != "Player")
                            {
                                attackstatecount = 1000000;
                                attacking        = false;
                                if (walkingspeed > 9)
                                {
                                    animator.SetBool("fastwalk", true);
                                }
                                else
                                {
                                    animator.SetBool("forwards", true);
                                }
                                animator.SetBool("walkleft", false);
                                animator.SetBool("walkright", false);
                                animator.SetBool("standing", false);
                                navvy.speed = walkingspeed;

                                animator.speed = walkingspeed / 3.5f;
                            }
                        }
                        if (Vector3.Distance(gameObject.transform.position, playermovement.target) > 3.5f)
                        {
                            attackstatecount = 1000000;
                            attacking        = false;
                            if (walkingspeed > 9)
                            {
                                animator.SetBool("fastwalk", true);
                            }
                            else
                            {
                                animator.SetBool("forwards", true);
                            }
                            animator.SetBool("walkleft", false);
                            animator.SetBool("walkright", false);
                            animator.SetBool("standing", false);
                            navvy.speed = walkingspeed;

                            animator.speed = walkingspeed / 3.5f;
                        }
                    }
                }
                if (state == destinations)
                {
                    pushtest();
                    if (!attacking)
                    {
                        navvy.SetDestination(destinat.transform.position);



                        animator.SetBool("forwards", true);

                        animator.SetBool("standing", false);
                        navvy.speed = walkingspeed;
                        gameObject.GetComponent <enemyik>().aiming = false;
                        animator.speed = walkingspeed / 3.5f;
                    }
                    if (attacking)
                    {
                        bulletcount++;
                        activatedcounter++;
                        if (bulletcount > bulletinterval)
                        {
                            bulletinterval = Random.Range(30, 200);
                            gun.Shoot();
                            bulletcount = 0;
                        }
                        gameObject.transform.LookAt(playermovement.target);
                        if (Vector3.Distance(gameObject.transform.position, playermovement.target) < destinationsdistance)
                        {
                            attacking = false;
                        }
                    }
                }
            }

            /*if (activated)
             * {
             *  if (recoiling)
             *  {
             *      if (kicked)
             *      {
             *          gameObject.transform.position = gameObject.transform.position + knockbackdirection;
             *      }
             *      recoilcount++;
             *      if (recoilcount > stunlockduration)
             *      {
             *          recoiling = false;
             *          animator.SetBool("recoiling", false);
             *          // gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled  = true;
             *          navvy.speed = walkingspeed;
             *          animator.speed = walkingspeed / 3.5f;
             *          recoilcount = 0;
             *          kicked = false;
             *      }
             *
             *  }
             *  if (!recoiling)
             *  {
             *      Vector3 targetDir = playermovement.target - gameObject.transform.position;
             *
             *
             *      if (attacking == true)
             *      {
             *          hitter.active = true;
             *          navvy.speed = 0;
             *          gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, Quaternion.LookRotation(new Vector3(targetDir.x, 0, targetDir.z), Vector3.up), 0.01f);
             *          if (Vector3.Distance(gameObject.transform.position, playermovement.target) > 1.5)
             *          {
             *              attacking = false;
             *              animator.SetBool("attacking", false);
             *              //gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled  = true;
             *
             *
             *          }
             *      }
             *      if (attacking == false)
             *      {
             *          navvy.speed = walkingspeed;
             *          hitter.active = false;
             *          this.gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().destination = playermovement.target;
             *          if (Vector3.Distance(gameObject.transform.position, playermovement.target) < 1.5)
             *          {
             *              attacking = true;
             *              animator.SetBool("attacking", true);
             *              animator.speed = 1;
             *              //gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
             *              navvy.speed = 0;
             *          }
             *
             *      }
             *
             *
             *  }
             *
             *
             * }*/
            if (twodee)
            {
                gameObject.transform.position = new Vector3(0, gameObject.transform.position.y, gameObject.transform.position.z);
            }
        }
    }