void SwapWeapons(int weaponSlot) { if (!mainCamera.transform.Find("Weapon Slot " + weaponSlot).GetChild(0)) { // Must not have a gun in the specified weapon slot. Don't swap. return; } if (gunController.zoomedIn) { StartCoroutine(gunController.Zoom()); } // Set current held weapon model to inactive. gunController.canFire = true; // Indicates to current gunController to cancel reload. gunController.animations.Stop(); // Stop any animations, particularly bullet chambering animations. mainCamera.transform.Find("Weapon Slot " + currWeaponSlotHeld).GetChild(0).GetChild(0).gameObject.SetActive(false); Transform newWeapon = mainCamera.transform.Find("Weapon Slot " + weaponSlot).GetChild(0); // Set the new weapon model to active. newWeapon.GetChild(0).gameObject.SetActive(true); // Get reference to gunController for new weapon and reset transform. gunController = newWeapon.GetComponent <GunController>(); gunController.resetTransform(); currWeaponSlotHeld = weaponSlot; // Update UI. ammoUI.setAmmoCount(gunController.currAmmoInClip, gunController.currSpareAmmo); if (gunController.currAmmoInClip > 0) { StartCoroutine(bulletTimerUI.RunTimer(0.0f, "full")); } else { StartCoroutine(bulletTimerUI.RunTimer(0.0f, "empty")); } }
public void Fire() { if (currAmmoInClip > 0) { if (attachedToPlayer) { applyRecoil(recoilAmount, recoilYBias); } gunSounds.PlayOneShot(gunFireSound, gunFireVolume); animations.Play(gameObject.name + " Shot"); // If last bullet is being fired, don't do chamber bullet animation nor play . if (currAmmoInClip != 1) { if (chamberBullet) { StartCoroutine(ChamberBullet()); } if (attachedToPlayer) { StartCoroutine(bulletTimerUI.RunTimer(firePause)); } } else if (attachedToPlayer) { StartCoroutine(bulletTimerUI.RunTimer(firePause, "empty")); } RaycastHit hit; if (Physics.Raycast(transform.parent.parent.position, transform.up, out hit)) { if (ammoType.Equals("bullet")) { if (hit.collider.gameObject.CompareTag("Enemy")) { Health health = hit.collider.gameObject.transform.parent.GetComponent <Health>(); health.TakeDamage(damage); } } else if (ammoType.Equals("object")) { if (hit.collider.gameObject.CompareTag("Ground")) { Instantiate(objectToSpawn, hit.point, Quaternion.identity); } } } currAmmoInClip -= 1; nextFire = Time.time + firePause; if (attachedToPlayer) { ammoUI.setAmmoCount(currAmmoInClip, currSpareAmmo); } } else { // Clip must be empty. Play appropriate sound. gunSounds.PlayOneShot(emptyGunSound, emptyGunVolume); // Prevent the spamming of the emptyGunSound sound effect. nextFire = Time.time + firePause; } }