diff options
author | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
commit | 6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch) | |
tree | b38119d2acf0a982cb67e381f146924b9bfc3b3f /Gun.cs |
+init
Diffstat (limited to 'Gun.cs')
-rw-r--r-- | Gun.cs | 94 |
1 files changed, 94 insertions, 0 deletions
@@ -0,0 +1,94 @@ +using UnityEngine; + +public class Gun : MonoBehaviour +{ + public GameObject projectile; + + public float rateOfFire = 0.1f; + + public bool auto = true; + + public int bulletsInMag = 30; + + public float reloadTime = 0.5f; + + public float ADSFOV = 60f; + + [HideInInspector] + public Rigidbody rig; + + private Transform shootPosition; + + private Recoil recoil; + + private AddScreenShake shake; + + private float counter; + + public float extraSpread; + + public float addForceToAllSpawnedRigs; + + private ParticleSystem part; + + private GunSFX sfx; + + private void Start() + { + rig = GetComponent<Rigidbody>(); + sfx = GetComponent<GunSFX>(); + recoil = GetComponent<Recoil>(); + shake = GetComponent<AddScreenShake>(); + shootPosition = GetComponentInChildren<ShootPosition>().transform; + part = GetComponentInChildren<ParticleSystem>(); + } + + private void Update() + { + counter += Time.deltaTime; + } + + public void Shoot(Transform shooter, bool ADS) + { + if (counter < rateOfFire) + { + return; + } + counter = 0f; + if ((bool)part) + { + part.Play(); + } + GameObject gameObject = Object.Instantiate(projectile, shootPosition.position, shootPosition.rotation); + RandomizeRotation component = gameObject.GetComponent<RandomizeRotation>(); + if ((bool)component) + { + component.spread += extraSpread; + } + SpawnerHolder component2 = gameObject.GetComponent<SpawnerHolder>(); + if ((bool)component2) + { + component2.spawner = shooter; + } + if ((bool)recoil) + { + recoil.AddRecoil(ADS); + } + if ((bool)shake) + { + shake.DoShake(); + } + if ((bool)sfx) + { + sfx.PlayShootSound(); + } + if (addForceToAllSpawnedRigs != 0f) + { + Rigidbody[] componentsInChildren = gameObject.GetComponentsInChildren<Rigidbody>(); + foreach (Rigidbody rigidbody in componentsInChildren) + { + rigidbody.AddForce(shootPosition.forward * addForceToAllSpawnedRigs, ForceMode.VelocityChange); + } + } + } +} |