Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class gun : MonoBehaviour
- {
- /*[Header("Weapon prefab")]
- public GameObject wepaonInHand;
- public GameObject pickUpWeapon;*/
- [Header("Weapon Damage Info")]
- // this is what does damage to objects that have the "Target" script on them
- public float damage = 10f;
- public float range = 100f;
- public float impactForce = 30f;
- public float fireRate = 15f;
- public enum Mode { Mode1, Mode2, Mode3 }
- [Header("Weapon Ammo & Info")]
- // this is the ammo information for the gun this script is attacted to.
- public int maxAmmo = 15;
- public int currentAmmo;
- public int maxNumberOfClips = 1;
- public int currentClips = 0;
- [Header("Weapon Recoil Info")]
- // this is the recoil information you can play around with this to setup the recoil for your gun
- public float recoil;
- public float maxRecoilX = 10;//Play Around with these Variables
- public float maxRecoilY = 10;
- public float recoilSpeed = 5;
- [Header("Weapon Ejector Info")]
- // the ejector for the bullet prefab, make sure that you orientate it so that it pops out the ejector correctly.
- public float myforce;
- public Transform bulletEject;
- public GameObject shellCasing;
- [Header("Weapon Sway Info 1")]
- // this is the weapon sway that you have. play around with these numbers
- public float mouseX;
- public float mouseY;
- public float speed;
- Quaternion rotationSpeed;
- [Header("Weapon Sway Info 2")]
- // this is the weapon sway that you have. play around with these numbers
- public float tiltAngle = 4.0f;
- public float tiltAmount = 0.001f;
- public float smoothComplexity = 3.0f;
- [Header("Weapon Aim Info")]
- //aiming with your weapon is done via script by using its vector 3 position.
- public float scopedFOV = 15f;
- public float normalFOV;
- public Vector3 aimDownSight;
- public Vector3 hipFire;
- [Header("Weapon Camera Info")]
- // just need both of your cameras here , if your new to unity please look at how my player prefab in the demo folders. is setup to make your own.
- public Camera mainCamera;
- public Camera fpsCam;
- [Header("Weapon Particle Systems Info")]
- // gotta make those particle systems for your weapons
- public ParticleSystem muzzleFlash;
- public GameObject impactPrefab;
- public GameObject impactPrefabmetal;
- public GameObject impactPrefabwood;
- public GameObject impactPrefabwater;
- public GameObject impactPrefabstone;
- public GameObject impactPrefabflesh;
- public GameObject impactPrefabsand;
- private GameObject impactGO;
- [Header("Weapon Audio Info")]
- // this is what makes some noise when your gun does somthing.
- public AudioSource audioSource;
- public AudioClip gunShotFire;
- public AudioClip gunShotEmpty;
- [Header("Weapon Reloading Info")]
- //your wepaon reloading informations , play around with it to make different reloading times.
- public float nextTimeToFire = 0f;
- public float reloadTime = 1f;
- public bool isReloading = false;
- [Header("Weapon Animator Info")]
- //animator for some animations on your weapon.
- public Animator animator;
- [Header("UI Manager")]
- //UI information thats is not being used at the momment.
- // public uiManager uimanager;
- public GameObject crossHair;
- /// <summary>
- /// all of my public variables used in this script
- ///
- /// </summary>
- private void Start()
- {
- // uimanager.refreshAmmo(currentAmmo);
- audioSource = GetComponent<AudioSource>();
- // Start edits //
- if (currentAmmo <= 0 && maxNumberOfClips > 0)
- {
- currentClips = maxNumberOfClips;
- currentAmmo = maxAmmo;
- // If you want to take a clip away at start (ie. player has just loaded one in)
- // uncomment the thing below
- currentClips -= 1;
- }
- // End edits //
- }
- public void AddRecoil()
- {
- recoil += 0.01f;
- }
- void Recoil() // this is the function to add recoil to the weapon " gun "
- {
- if (recoil > 0)
- {
- var maxRecoil = Quaternion.Euler(maxRecoilX + Random.Range(-10, 10), maxRecoilY + Random.Range(-10, 10), 0);
- transform.localRotation = Quaternion.Slerp(transform.localRotation, maxRecoil, Time.deltaTime * recoilSpeed); // This controls the actual recoil
- transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z); // This one also
- recoil -= Time.deltaTime;
- }
- else
- {
- recoil = 0;
- var minRecoil = Quaternion.Euler(0, 0, 0);
- transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, Time.deltaTime * recoilSpeed / 2);
- transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
- }
- }
- private void OnEnable() // private function for calling the reloading
- {
- isReloading = false;
- animator.SetBool("Reloading", false);
- }
- // Update is called once per frame
- void Update()
- {
- /*if (Input.GetKeyDown("e"))
- {
- wepaonInHand.SetActive(false);
- Instantiate(pickUpWeapon, transform.position, transform.rotation);
- }*/
- Recoil();
- // uimanager.refreshAmmo(maxNumberOfClips);
- if (isReloading)
- return;
- if (currentAmmo <= 0)
- {
- StartCoroutine(Reload());
- return;
- }
- // Start edits //
- if (currentAmmo > 0 && Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
- {
- nextTimeToFire = Time.time + 1f / fireRate;
- Shoot();
- Recoil();
- }
- {
- float tiltX = Input.GetAxis("Mouse X") * tiltAngle;
- float tiltY = Input.GetAxis("Mouse Y") * tiltAngle;
- Quaternion target = Quaternion.Euler(tiltY, tiltX, 0);
- transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smoothComplexity);
- }
- //
- // mouseX = Input.GetAxis("Mouse X");
- //mouseY = Input.GetAxis("Mouse Y");
- //rotationSpeed = Quaternion.Euler(-mouseY, mouseX, 0);
- //transform.localRotation = Quaternion.Slerp(transform.localRotation, rotationSpeed, speed * Time.deltaTime);
- // End edits //
- if (Input.GetButtonDown("Fire2"))
- {
- transform.localPosition = aimDownSight;
- crossHair.SetActive(false);
- normalFOV = mainCamera.fieldOfView;
- mainCamera.fieldOfView = scopedFOV;
- }
- if (Input.GetButtonUp("Fire2"))
- {
- transform.localPosition = hipFire;
- crossHair.SetActive(true);
- mainCamera.fieldOfView = normalFOV;
- }
- }
- IEnumerator Reload()
- {
- Debug.Log("Reloading....");
- // Start edits //
- if (currentClips == 0)
- {
- // No clips? Then no reload for you.
- noMoreBullets();
- Debug.Log("Reload failed; no clips!");
- }
- else
- {
- isReloading = true;
- currentClips -= 1; // Take a clip away
- yield return new WaitForSeconds(reloadTime);
- animator.SetBool("Reloading", false);
- currentAmmo = maxAmmo;
- isReloading = false;
- }
- // End edits //
- }
- // Why is this public? //
- void noMoreBullets()
- {
- if (Input.GetButtonDown("Fire1"))
- {
- audioSource.PlayOneShot(gunShotEmpty, 0.7F);
- }
- return;
- }
- void impactParticleSystems(GameObject particlesystem)
- {
- }
- void Shoot()
- {
- muzzleFlash.Play();
- AddRecoil();
- RaycastHit hit;
- currentAmmo--;
- // UI refresh
- // uimanager.refreshAmmo(currentAmmo);
- GameObject shellCasings = Instantiate(shellCasing, bulletEject.position, bulletEject.rotation);
- shellCasings.GetComponent<Rigidbody>().AddForce(bulletEject.right * myforce);
- Destroy(shellCasings, 2f);
- audioSource.PlayOneShot(gunShotFire, 0.7F);
- if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
- {
- Debug.Log(hit.transform.name);
- switch (hit.collider.tag)
- {
- case "flesh":
- impactGO = impactPrefabflesh;
- break;
- case "sand":
- impactGO = impactPrefabsand;
- break;
- case "wood":
- impactGO = impactPrefabwood;
- break;
- case "metal":
- impactGO = impactPrefabmetal;
- break;
- case "stone":
- impactGO = impactPrefabstone;
- break;
- case "water":
- impactGO = impactPrefabwater;
- break;
- default:
- impactGO = impactPrefab;
- break;
- }
- Destructiable target = hit.transform.GetComponent<Destructiable>();
- if (target != null)
- {
- target.TakeDamage(damage);
- }
- if (hit.rigidbody != null)
- {
- hit.rigidbody.AddForce(hit.normal * impactForce);
- }
- GameObject impactgo = Instantiate(impactGO, hit.point, Quaternion.LookRotation(hit.normal));
- Destroy(impactgo, 2f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement