void SetEquippedGun() { int i = 0; foreach (Transform gun in gunHolder) { gun.gameObject.SetActive(i == (int)equippedGunType); if (i == (int)equippedGunType) { equippedGun = gun.GetComponent <Gun>(); currentCrosshair = gun.GetComponent <DrunkCrosshair>(); onGunSwap.Invoke(); } i++; } }
// Private Methods void Shoot() { muzzleFlash.Play(); consecutiveShots++; consecutiveShotsAtShot = consecutiveShots; lastFireTime = Time.unscaledTime; bulletsInGun--; if (bullets.GetLength(0) > 0) { bullets[bulletsInGun].SetActive(false); } float hitProbability = DrunkCrosshair.GetHitProbability(); Vector3 direction = (fpsCamera.ScreenToWorldPoint(DrunkCrosshair.Position) - fpsCamera.transform.position).normalized; RaycastHit hit; Debug.DrawRay(fpsCamera.transform.position, direction * range, Color.red, 3); if (Physics.Raycast(fpsCamera.transform.position, direction, out hit, range, shootingLayerMask) && hitProbability > 50f) { Life targetLife = hit.transform.GetComponent <Life>(); Rigidbody targetRigidbody = hit.transform.GetComponent <Rigidbody>(); if (targetLife) { targetLife.TakeDamage(); onShotTarget.Invoke(); } if (targetRigidbody) { float forcePercentage = 1f - (transform.position - hit.transform.position).sqrMagnitude / (range * range); targetRigidbody.AddForceAtPosition(-hit.normal * impactForce * forcePercentage, hit.point); } } onShot.Invoke(); }