Seek And Destroy

Ok, so our enemy NPC has a patrol route around the arena. Next step is to make the enemy target the player when he gets to close and cause damage when the enemy collides with the player.

I think I'm starting to get the hang of some of this. So what we do in the code is use the find the position of the player by creating a GameObject to find the player. Then the players position is added to the onTriggerEnter(Collider other) function as a condition. Essentially if the player is detected by the enemy, the enemy will change its position to the player.

private void OnTriggerEnter(Collider other)
    {
        // Checks to see if the player has entered the trigger zone
        if(other.name == "Player")
        {
            _agent.destination = Player.position;
            Debug.Log("Player detected - attack");
        }
    }

After onTriggerEnter fires and the enemy NPC goes to the players location, the Update function is run and the enemy goes to the next position in its patrol route. Now, its cool that I can get the enemy to move to the players position. How do I make the player pay for his blunder???

Ok, so this is done using another onCollisionEnter function. First off we need a variable to store the reference to the instance of GameBehavior we have in the scene. Next, the GameBehavior script that is attached to the Game_Manager object in the scene is found using GameObject.Find("Game_Manager") call from PlayerBehavior. This result is stored in gameManager which is then used in the onCollisionEnter function where a call to Game Managers HP function is called reducing the hit point value by 1.

private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Enemy")
        {
            _gameManager.HP -= 1;
        }
    }

Remember back when we setup the player with the ability to shoot bullets??? Now we get to defend ourselves from this heinous enemy NPC. When we shoot the bullet we need a way to detect bullet collisions with the enemy. Again a NavMesh Collision event is used to trigger hits.

First off we need to set how many lives our enemy has before he loses. We set this number to 3 using a private int called _lives. We then create a public variable called EnemyLives that will let us control how EnemyLives is referenced and set. The get property always returns _lives. A private set property is then used to assign the new value of EnemyLives to _lives. That way we can keep both in sync.

private int _lives = 3;
    public int EnemyLives
    {
        get { return _lives; }
        private set
        {
            _lives = value;
            if (_lives <= 0)
            {
                Destroy(this.gameObject);
                Debug.Log("Enemy Down");
            }
        }
    }

Next an if statement is used to check to see if _lives is less than or equal to 0. If this is true then the enemy is should be dead. If the enemy is dead, we destroy the enemy and print a console message. Because we're now shooting bullets at the enemy we need to add a check for the bullet collisions on the enemy using.... you guessed it, an OnCollisionEnter() call. In this call, we reduce the number of EnemyLives by 1 and print a console message.

private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Bullet(Clone)")
        {
            EnemyLives -= 1;
            Debug.Log("Critical Hit!");
        }
    }

Now the game is on. The enemy can hit the player and the player can fight back. Now that some game play.