Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class RaycastShoot : MonoBehaviour
- {
- public float damage = 10f;
- public float range = 100f;
- public float impactForce = 30f;
- public float fireRate = 15f;
- private bool isReloading = false;
- public Animator animator;
- public Text currentammoText;
- public Text leftammoText;
- private bool isActive = true;
- private int maxAmmo = 8;
- private int leftAmmo;
- private int currentAmmo;
- public ParticleSystem muzzleFlash;
- public GameObject impactEffect;
- public ParticleSystem stuckFlash;
- public float reloadTime = 1.5f;
- private float nextTimeToFire = 0f;
- public Camera fpsCam;
- Vector3 recoilSmoothDampVelocity;
- void Start()
- {
- currentAmmo = maxAmmo;
- leftAmmo = 64;
- currentAmmo = 8;
- }
- void Update()
- {
- if (isReloading)
- return;
- if (currentAmmo <= 0)
- {
- StartCoroutine(Reload());
- return;
- }
- if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
- {
- nextTimeToFire = Time.time + 1f / fireRate;
- Shoot();
- }
- transform.localPosition = Vector3.SmoothDamp(transform.localPosition, Vector3.zero, ref recoilSmoothDampVelocity, .1f);
- }
- void OnEnable()
- {
- isReloading = false;
- animator.SetBool("Reloading", false);
- }
- IEnumerator Reload()
- {
- if (isActive == true)
- {
- isReloading = true;
- Debug.Log("Reloading...");
- FindObjectOfType<AudioManager>().Play("Reload1");
- leftAmmo = leftAmmo - 8;
- leftammoText.text = "" + leftAmmo.ToString();
- currentAmmo = currentAmmo + 8;
- currentammoText.text = "" + currentAmmo.ToString();
- animator.SetBool("Reloading", true);
- yield return new WaitForSeconds(reloadTime - .25f);
- animator.SetBool("Reloading", false);
- yield return new WaitForSeconds(.25f);
- currentAmmo = maxAmmo;
- isReloading = false;
- }
- }
- void Shoot()
- {
- if (isActive == true)
- {
- muzzleFlash.Play();
- currentAmmo = currentAmmo - 1;
- currentammoText.text = "" + currentAmmo.ToString();
- FindObjectOfType<AudioManager>().Play("GunShot1");
- RaycastHit hit;
- if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
- {
- Debug.Log(hit.transform.name);
- ShootableBox target = hit.transform.GetComponent<ShootableBox>();
- if (target != null)
- {
- target.TakeDamage(damage);
- }
- if (hit.rigidbody != null)
- {
- hit.rigidbody.AddForce(-hit.normal * impactForce);
- }
- transform.localPosition -= Vector3.forward * .2f;
- GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
- Destroy(impactGO, 2f);
- }
- }
- if (currentAmmo == 0 && leftAmmo == 0)
- {
- isActive = false;
- }
- if(Input.GetButtonDown("Fire1") && isActive == false)
- {
- FindObjectOfType<AudioManager>().Play("Stuck1");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement