Example #1
0
 // Reload function
 public void Reload()
 {
     // Test if reload was successful
     if (activeGun_.Reload())
     {
         // Reset crosshair spread on reload
         aimSpread_ = 0;
     }
 }
Example #2
0
    // Shoot function
    void Shoot()
    {
        // Can shoot at this moment (shoot-enable timer elapsed) and clip is not empty
        if (activeGun_.CanShoot())
        {
            // Call active gun shoot function
            activeGun_.Shoot();

            // Add jitter to the shootRay
            Vector3 jitter = Random.insideUnitSphere / 25.0f; // TODO: Change difficulty by altering jitter size
            shootRay_.origin    = enemyPosition_;
            shootRay_.direction = playerDirection_.normalized + jitter;

            // Test shoot ray against player
            if (Physics.Raycast(shootRay_, out shootHit_, activeGun_.GetRange()))
            {
                // Player was hit
                if (shootHit_.collider.tag == "Player")
                {
                    // Get player health component
                    PlayerHealth playerHealth = shootHit_.collider.GetComponent <PlayerHealth>();
                    if (playerHealth != null)
                    {
                        // Hurt player
                        playerHealth.TakeDamage(activeGun_.GetDamagePerShot() / 2);

                        // Set player alive flag (get value from player health script)
                        isPlayerAlive_ = !playerHealth.isDead_;
                    }
                }
            }
        }

        // Reload if needed
        if (activeGun_.GetClipBullets() == 0)
        {
            // Disable effects after last bullet
            activeGun_.DisableEffects();
            // Reload
            activeGun_.Reload();
        }
    }