Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using TMPro;
- public class GunSystem : MonoBehaviour
- {
- //Animations
- //Gun stats
- public int damage;
- public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
- public int magazineSize, bulletsPerTap;
- public bool allowButtonHold;
- int bulletsLeft, bulletsShot;
- //bools
- bool shooting, readyToShoot, reloading;
- //Reference
- public Camera fpsCam;
- public Transform attackPoint;
- public RaycastHit rayHit;
- public LayerMask whatIsEnemy;
- //Graphics
- public GameObject muzzleFlash, bulletHoleGraphic;
- public float camShakeMagnitude, camShakeDuration;
- public TextMeshProUGUI text;
- private void Awake()
- {
- bulletsLeft = magazineSize;
- readyToShoot = true;
- }
- private void Update()
- {
- MyInput();
- //SetText
- text.SetText(bulletsLeft + " / " + magazineSize);
- }
- private void MyInput()
- {
- if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
- else shooting = Input.GetKeyDown(KeyCode.Mouse0);
- if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
- //Shoot
- if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
- {
- bulletsShot = bulletsPerTap;
- Shoot();
- }
- }
- private void Shoot()
- {
- readyToShoot = false;
- //Spread
- float x = Random.Range(-spread, spread);
- float y = Random.Range(-spread, spread);
- //Calculate Direction with Spread
- Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);
- //Play Animation
- //RayCast
- if (Physics.Raycast(fpsCam.transform.position, direction, out rayHit, range, whatIsEnemy))
- {
- Debug.Log(rayHit.collider.name);
- TriggerAppear platform = rayHit.collider.gameObject.GetComponent<TriggerAppear>();
- AIHealth health = rayHit.collider.gameObject.GetComponent<AIHealth>();
- if(platform != null)
- {
- platform.Platform.SetActive(true);
- }
- if(rayHit.collider.gameObject.tag == "Enemy")
- {
- if(health != null)
- {
- health.Health -= damage;
- }
- }
- }
- //ShakeCamera
- //Graphics
- Instantiate(bulletHoleGraphic, rayHit.point + rayHit.normal * 0.002f, Quaternion.LookRotation(rayHit.normal), rayHit.collider.gameObject.transform);
- Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
- bulletsLeft--;
- bulletsShot--;
- Invoke("ResetShot", timeBetweenShooting);
- if (bulletsShot > 0 && bulletsLeft > 0)
- Invoke("Shoot", timeBetweenShots);
- }
- private void ResetShot()
- {
- readyToShoot = true;
- }
- private void Reload()
- {
- reloading = true;
- Invoke("ReloadFinished", reloadTime);
- }
- private void ReloadFinished()
- {
- bulletsLeft = magazineSize;
- reloading = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement