Exemple #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        //if the player character is far enough away, start moving
        if (Vector3.Distance(transform.position, target.position) > 3.0f)
        {
            isMoving = true;
            GetComponent <Rigidbody2D>().isKinematic = false;
        }


        //Checks how far the enemy is from the Hero. If it is close, change it to a kinematic rigidbody
        //to stop it from moving the Hero and other enemies. This also stops the Hero from moving the enemies.
        //Otherwise move toward the Hero.

        if (isMoving)
        {
            if (Vector3.Distance(transform.position, target.position) <= 1.0f)
            {
                rb.isKinematic = true;
                return;
            }
            else
            {
                rb.isKinematic = false;
                //Moves the enemies position towards the target("Hero") against a fixed speed
                transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
            }
        }

        //Kill the enemy if their health drops to 0 or below
        if (healthSystem.GetHealth() <= 0)
        {
            Destroy(gameObject);
            PlayerScore.FindObjectOfType <PlayerScore>().SendMessage("AddScore");
            PlayerScore.FindObjectOfType <PlayerScore>().SendMessage("AddGold");

            SoundManager.Instance.RandomPlayEnemyDeadSource(enemyDeadSound1, enemyDeadSound2);
            Debug.LogFormat(gameObject.name + " was killed");
        }

        //Attack the hero if in range
        if (playerInRange == true)
        {
            if (attackTimeCounter <= 0)
            {
                attackTimeCounter = attackTime;
                attacking         = true;

                player.GetComponent <PlayerHealth>().TakeDamage(damage);
                Debug.LogFormat("Counter reset");
            }
            else if (attackTimeCounter > 0)
            {
                attackTimeCounter -= Time.deltaTime;
            }
        }

        if (takeDamageTimeCounter > 0)
        {
            takeDamageTimeCounter -= Time.deltaTime;
        }



        /*****************************************
        *  Not needed for sprint 1
        *****************************************/
        /*
         * //Changes enemy facing direction towards the player
         * //left and right
         * if (enemy.transform.position.x > 0.0f || enemy.transform.position.x < -0.0f)
         * {
         *  playerMoving = true;
         *  lastMove = new Vector2(enemy.transform.position.x, 0f);
         * }
         *
         * if (enemy.transform.position.y > 0.0f || enemy.transform.position.y < -0.0f)
         * {
         *  playerMoving = true;
         *  lastMove = new Vector2(0f, enemy.transform.position.y);
         * }
         *
         * anim.SetFloat("MoveX", enemy.transform.position.x);
         * anim.SetFloat("MoveY", enemy.transform.position.y);
         * anim.SetBool("isMoving", playerMoving);
         * anim.SetFloat("LastMoveX", lastMove.x);
         * anim.SetFloat("LastMoveY", lastMove.y);
         */
    }