Example #1
0
    //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    //plays death sound and disables running animation
    //will destroy the enemies collider so the player cant deal damage to the enemy after they're dead
    //finds the enemies death position and stores it in healthPackPos which is used as the spawn point for all power-ups
    //creates 3 random number generators, one for each powerup
    //healthpack has 5% chance to spawn at enemies death location, doublexp is 2% chance, and nuke is 2% too
    //enemy is then set to kinematic to stop them moving and their death animation will play before destroying them after 2 seconds
    //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    void Die()
    {
        anim.SetBool("isRunning", false);
        audio.Play();
        Destroy(gameObject.GetComponent <CapsuleCollider> ());
        Vector3 healthPackPos = new Vector3(transform.position.x, transform.position.y + 1, transform.position.z);

        System.Random rand             = new System.Random();
        int           healthPackChance = rand.Next(0, 100);
        int           doubleXpChance   = rand.Next(0, 100);
        int           nukeChance       = rand.Next(0, 100);

        if (healthPackChance >= 95)
        {
            Instantiate(healthPack, healthPackPos, transform.rotation);
        }

        if (doubleXpChance >= 98)
        {
            Instantiate(doubleScorePack, healthPackPos, transform.rotation);
        }
        if (nukeChance >= 98)
        {
            Instantiate(nukeObject, healthPackPos, transform.rotation);
        }

        rb.isKinematic = true;
        if (hs.isDoubleXP)
        {
            sc.addToScore(20);
        }

        if (!(hs.isDoubleXP))
        {
            sc.addToScore(10);
        }

        spawn.removeFromCurrent();
        anim.SetTrigger("isDead");
        Destroy(gameObject, 2f);
    }