Example #1
0
    // Update is called once per frame
    void Update()
    {
        //If the enemy manages to go out of camera view respawn it
        if (gameObject.transform.position.x > enemyManager.boundPos.x || gameObject.transform.position.y > enemyManager.boundPos.y)
        {
            //Set the enemy to dead
            enemyManager.SetIsDead(true);
        }

        //If the player manages to kill the enemy respawn it
        if (enemyManager.GetIsDead() == true)
        {
            //reset the enemies transform
            Spawn();
            //Set the enemy to alive
            enemyManager.SetIsDead(false);
        }
    }
    //Checks for a touch on the enemy and responds by deducting health
    void OnTouch()
    {
        //Raycast
        Ray          ray = Camera.main.ScreenPointToRay(m_touch);
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);

        //If there is a hit
        if (hit)
        {
            //Checkt the gameObject's tag
            if (hit.collider.gameObject.tag == "Enemy")
            {
                //Get access to the gameObject's enemyManager script
                enemyManager = hit.collider.gameObject.GetComponent <EnemyManagerScript>();
                // - health
                currentHealth = enemyManager.GetHealth();
                currentHealth--;
                //Set the new health
                enemyManager.SetHealth(currentHealth);


                //Check if enemy is dead
                if (enemyManager.GetHealth() <= 0)
                {
                    enemyManager.SetIsDead(true);
                }

                //If enemy is dead reset
                if (enemyManager.GetIsDead() == true)
                {
                    //Reset the enemies position
                    hit.collider.gameObject.transform.position = enemyManager.GetSpawnPos();
                    //Reset enemies health
                    enemyManager.ResetHealth();
                    //Set enemy to alive
                    enemyManager.SetIsDead(false);
                }
            }
        }
    }