Example #1
0
    // Update function
    void Update()
    {
        // Update ammo text
        ammoText_.text = activeGun_.GetClipBullets().ToString() + "/" + activeGun_.GetTotalBullets().ToString();

        // Decrease crosshair spread and clamp it
        aimSpread_ -= Time.deltaTime * 1.2f;
        aimSpread_  = Mathf.Clamp(aimSpread_, 1.0f, 2.0f);
    }
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();
        }
    }