//private void ShootPhysics() //{ // ray.origin = camera.position; // ray.direction = camera.forward; // if (Physics.Raycast(ray, out RaycastHit hit, 100)) // { // Instantiate(hitParticlePrefab, hit.point, Quaternion.identity); // if (hit.collider.attachedRigidbody != null) // { // var hittable = hit.collider.gameObject.GetComponent<HittableBodyPart>(); // if (hittable != null) // { // hittable.GotHit(ray.direction, gunImpact); // } // else // { // hit.collider.attachedRigidbody.AddForce(ray.direction * gunImpact, ForceMode.Impulse); // } // } // } //} private void ShootPhysics() { bool hitLivingEnemy = false; bool hitWall = false; RaycastHit penetratingHit = default; HittableBodyPart hittable = null; ray.origin = camera.position; ray.direction = camera.forward; RaycastHit[] hits = Physics.RaycastAll(ray, 100); doTheVFX = true; if (hits.Length > 0) { for (int i = 0; i < hits.Length; i++) { var hit = hits[i]; if (hit.collider.attachedRigidbody != null) { hittable = hit.collider.gameObject.GetComponent <HittableBodyPart>(); if (hittable != null) { hitLivingEnemy = true; } else { hit.collider.attachedRigidbody.AddForce(ray.direction * gunImpact, ForceMode.Impulse); } } else if (hit.collider.gameObject.tag == "Wall") { if (i == 0) { hitWall = true; penetratingHit = hit; } } } } if (hitLivingEnemy && hitWall) { doTheVFX = false; FirePenetratingShot(penetratingHit, hittable); } else if (hitLivingEnemy) { hittable.GotHit(ray.direction, gunImpact); } else { Instantiate(hitParticlePrefab, hits[0].point, Quaternion.identity); } }
IEnumerator PenetratingShotRoutine(RaycastHit penetratingHit, Quaternion bulletRot, HittableBodyPart hittableBodyPart) { Vector3 targetPos = penetratingHit.point; yield return(new WaitForSeconds(2)); SlowMotion(0.2f); ShootVFX(); yield return(new WaitForSeconds(0.11f)); Transform bullet = Instantiate(bulletPrefab, nozzlePos.position, bulletRot).transform; float t = 0; bool triggered = false; Vector3 euler = bullet.rotation.eulerAngles; while (Vector3.Distance(bullet.position, targetPos) > 0.1f) { if (t < 0.05f) { t += Time.deltaTime; } else if (!triggered) { SlowMotion(0.5f); triggered = true; EventManager.TriggerEvent("Penetrating Shot Fired", bullet); } bullet.position = Vector3.MoveTowards(bullet.position, targetPos, Time.deltaTime * bulletSpeed); euler.z += Time.deltaTime * bulletSpeed * 100; bullet.eulerAngles = euler; yield return(null); } SlowMotion(1); EventManager.TriggerEvent("Penetrating Shot Hit"); Destroy(bullet.gameObject); Instantiate(wallPenetratingBulletSystemPrefab, penetratingHit.point, penetratingHit.transform.rotation); hittableBodyPart.GotHit(ray.direction, gunImpact); }