void DoHitFX(Vector3 pos, Quaternion rot, Collider col) { // Create FX at impact point / rotation if (HitFXPrefab) { GameObject impact = Instantiate(HitFXPrefab, pos, rot) as GameObject; BulletHole hole = impact.GetComponent <BulletHole>(); if (hole) { hole.TryAttachTo(col); } } // push object if rigidbody Rigidbody hitRigid = col.attachedRigidbody; if (hitRigid != null) { hitRigid.AddForceAtPosition(transform.forward * AddRigidForce, pos, ForceMode.VelocityChange); } // Damage if possible Damageable d = col.GetComponent <Damageable>(); if (d) { d.DealDamage(Damage); } }
public virtual void ApplyParticleFX(Vector3 position, Quaternion rotation, Collider attachTo) { if (HitFXPrefab) { GameObject impact = Instantiate(HitFXPrefab, position, rotation) as GameObject; // Attach bullet hole to object if possible BulletHole hole = impact.GetComponent <BulletHole>(); if (hole) { hole.TryAttachTo(attachTo); } } }
public virtual void Shoot() { // Has enough time passed between shots float shotInterval = Time.timeScale < 1 ? 0.3f : FiringRate; if (Time.time - lastShotTime < shotInterval) { return; } // Need to Chamber round into weapon if (!BulletInChamber && MustChamberRounds) { VRUtils.Instance.PlaySpatialClipAt(EmptySound, transform.position, 1f, 0.5f); return; } // Need to release slide if (ws != null && ws.LockedBack) { VRUtils.Instance.PlaySpatialClipAt(EmptySound, transform.position, 1f, 0.5f); return; } // Create our own spatial clip VRUtils.Instance.PlaySpatialClipAt(GunShotSound, transform.position, 1f); // Haptics if (thisGrabber != null) { input.VibrateController(0.1f, 0.2f, 0.1f, thisGrabber.HandSide); } // Use projectile if Time has been slowed bool useProjectile = Time.timeScale < 1; if (useProjectile) { GameObject projectile = Instantiate(ProjectilePrefab, MuzzlePointTransform.position, MuzzlePointTransform.rotation) as GameObject; Rigidbody projectileRigid = projectile.GetComponent <Rigidbody>(); projectileRigid.AddForce(MuzzlePointTransform.forward * ShotForce, ForceMode.VelocityChange); Projectile proj = projectile.GetComponent <Projectile>(); // Convert back to raycast if Time reverts if (proj) { proj.MarkAsRaycastBullet(); } // Make sure we clean up this projectile Destroy(projectile, 20); } else { // Raycast to hit RaycastHit hit; if (Physics.Raycast(MuzzlePointTransform.position, MuzzlePointTransform.forward, out hit, MaxRange, ValidLayers, QueryTriggerInteraction.Ignore)) { // Particle FX on impact Quaternion decalRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal); GameObject impact = Instantiate(HitFXPrefab, hit.point, decalRotation) as GameObject; BulletHole hole = impact.GetComponent <BulletHole>(); if (hole) { hole.TryAttachTo(hit.collider); } // push object if rigidbody Rigidbody hitRigid = hit.collider.attachedRigidbody; if (hitRigid != null) { float bulletForce = 1000; hitRigid.AddForceAtPosition(bulletForce * MuzzlePointTransform.forward, hit.point); } // Damage if possible IDamageable d = hit.collider.GetComponent <IDamageable>(); if (d != null) { d.DealDamage(Damage); } } } // Apply recoil if (weaponRigid != null && RecoilForce != Vector3.zero) { weaponRigid.AddForceAtPosition(RecoilForce, MuzzlePointTransform.position, ForceMode.VelocityChange); } // We just fired this bullet BulletInChamber = false; // Try to load a new bullet into chamber if (AutoChamberRounds) { chamberRound(); } else { EmptyBulletInChamber = true; } // Unable to chamber bullet, force slide back if (!BulletInChamber) { // Do we need to force back the receiver? slideForcedBack = ForceSlideBackOnLastShot; if (slideForcedBack && ws != null) { ws.LockBack(); } } // Store our last shot time to be used for rate of fire lastShotTime = Time.time; // Stop previous routine if (shotRoutine != null) { MuzzleFlashObject.SetActive(false); StopCoroutine(shotRoutine); } if (AutoChamberRounds) { shotRoutine = animateSlideAndEject(); StartCoroutine(shotRoutine); } else { shotRoutine = doMuzzleFlash(); StartCoroutine(shotRoutine); } }