// 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()); }
// Init function void Start() { // Get player transform matrix player_ = GameObject.FindGameObjectWithTag("Player").transform; // Get navigation mesh agent component nav_ = GetComponent <NavMeshAgent>(); // Get capsule component capsule_ = GetComponent <CapsuleCollider>(); // Get enemy health script enemyHealth_ = GetComponent <EnemyHealth>(); // Get player controller script playerController_ = GameObject.FindGameObjectWithTag("Player").GetComponent <PlayerControllerAnimated>(); // Get active gun component activeGun_ = GetComponentInChildren <SciFiRifle>(); // Get animator component anim_ = GetComponent <Animator>(); // Get walk audio component walkAudio_ = GetComponents <AudioSource>()[0]; // Get speech audio component speechAudio_ = GetComponents <AudioSource>()[1]; // Get environment layer mask for shooting environmentMask_ = LayerMask.GetMask("Environment"); // Create shoot ray shootRay_ = new Ray(); // Get minimum distance to be able to move and shoot minShootRange_ = activeGun_.GetRange() / 4.0f - 5.0f; // 10 // Get maximum distance to be able to shoot maxShootRange_ = activeGun_.GetRange() / 4.0f + 5.0f; // 20 // Clear distraction point distractionPoint_ = NO_DISTRACTION_SET; // Go to the next point in defined path GoToNextPoint(); }
// 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(); } }