Example #1
0
    // Shoot function
    public void Shoot()
    {
        // Cannot shoot at this moment (so fast) or clip is empty
        if (!activeGun_.CanShoot())
        {
            return;
        }

        // Increase crosshair spread while shooting
        aimSpread_ += Time.deltaTime * 13;

        // Shoot with active gun
        activeGun_.Shoot();

        // Setup shoot ray
        // Define jitter
        Vector3 jitter = Mathf.Pow(aimSpread_, 3) * Random.insideUnitSphere / 200.0f;

        // Set ray origin to camera position
        shootRay_.origin = playerCamera_.transform.position;
        // Set ray direction to camera forward vector and add the jitter
        shootRay_.direction = playerCamera_.transform.forward + jitter;

        // Test shoot ray against environment and enemy layers
        if (Physics.Raycast(shootRay_, out shootHit_, activeGun_.GetRange(), shootMask_))
        {
            // Shoot ray hit the enemy
            if (shootHit_.collider.tag == "Enemy")
            {
                // Get enemy health component
                EnemyHealth enemyHealth = shootHit_.collider.GetComponent <EnemyHealth>();
                if (enemyHealth != null)
                {
                    // Harm the enemy with the gun damage
                    enemyHealth.TakeDamage(activeGun_.GetDamagePerShot(), shootHit_.point);
                }
            }
            // Shoot ray hit the enemy's head
            else if (shootHit_.collider.tag == "EnemyHead")
            {
                // Get enemy health component
                EnemyHealth enemyHealth = shootHit_.collider.GetComponentInParent <EnemyHealth>();
                if (enemyHealth != null)
                {
                    // Harm the enemy with 5 times the gun damage
                    enemyHealth.TakeDamage(activeGun_.GetDamagePerShot() * 5, shootHit_.point);
                }
            }
        }

        // Set player's noise level to gun noise
        playerController_.SetNoiseLevel(activeGun_.GetNoiseLevel());
    }
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();
        }
    }