Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.94 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class gun : MonoBehaviour
  7. {
  8.     /*[Header("Weapon prefab")]
  9.     public GameObject wepaonInHand;
  10.     public GameObject pickUpWeapon;*/
  11.    
  12.  
  13.     [Header("Weapon Damage Info")]
  14. // this is what does damage to objects that have the "Target" script on them
  15.     public float damage = 10f;
  16.     public float range = 100f;
  17.     public float impactForce = 30f;
  18.     public float fireRate = 15f;
  19.     public enum Mode { Mode1, Mode2, Mode3 }
  20.    
  21.  
  22.     [Header("Weapon Ammo & Info")]
  23. // this is the ammo information for the gun this script is attacted to.
  24.     public int maxAmmo = 15;
  25.     public int currentAmmo;
  26.     public int maxNumberOfClips = 1;
  27.     public int currentClips = 0;
  28.  
  29. [Header("Weapon Recoil Info")]
  30. // this is the recoil information you can play around with this to setup the recoil for your gun
  31.     public float recoil;
  32.     public float maxRecoilX = 10;//Play Around with these Variables
  33.     public float maxRecoilY = 10;
  34.     public float recoilSpeed = 5;
  35.  
  36.  [Header("Weapon Ejector Info")]
  37.  // the ejector for the bullet prefab, make sure that you orientate it so  that it pops out the ejector correctly.
  38.     public float myforce;
  39.     public Transform bulletEject;
  40.     public GameObject shellCasing;    
  41.  
  42. [Header("Weapon Sway Info 1")]
  43. // this is the weapon sway that you have. play around with these numbers
  44.     public float mouseX;
  45.     public float mouseY;
  46.     public float speed;
  47.     Quaternion rotationSpeed;
  48.  
  49. [Header("Weapon Sway Info 2")]
  50. // this is the weapon sway that you have. play around with these numbers
  51.     public float tiltAngle = 4.0f;
  52.     public float tiltAmount = 0.001f;
  53.     public float smoothComplexity = 3.0f;
  54.  
  55. [Header("Weapon Aim Info")]
  56. //aiming with your weapon is done via script by using its vector 3 position.
  57.     public float scopedFOV = 15f;
  58.     public float normalFOV;
  59.     public Vector3 aimDownSight;
  60.     public Vector3 hipFire;
  61.    
  62.  
  63. [Header("Weapon Camera Info")]
  64. // 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.
  65.     public Camera mainCamera;
  66.     public Camera fpsCam;
  67.  
  68. [Header("Weapon Particle Systems Info")]
  69. // gotta make those particle systems for your weapons
  70.     public ParticleSystem muzzleFlash;
  71.     public GameObject impactPrefab;
  72.     public GameObject impactPrefabmetal;
  73.     public GameObject impactPrefabwood;
  74.     public GameObject impactPrefabwater;
  75.     public GameObject impactPrefabstone;
  76.     public GameObject impactPrefabflesh;
  77.     public GameObject impactPrefabsand;
  78.     private GameObject impactGO;
  79.  
  80.     [Header("Weapon Audio Info")]
  81. // this is what makes some noise when your gun does somthing.
  82.     public AudioSource audioSource;
  83.     public AudioClip gunShotFire;    
  84.     public AudioClip gunShotEmpty;
  85.  
  86.  
  87. [Header("Weapon Reloading Info")]
  88. //your wepaon reloading informations , play around with it to make different reloading times.
  89.     public float nextTimeToFire = 0f;
  90.     public float reloadTime = 1f;
  91.     public bool isReloading = false;
  92.  
  93. [Header("Weapon Animator Info")]
  94. //animator for some animations on your weapon.
  95.     public Animator animator;
  96.  
  97. [Header("UI Manager")]
  98. //UI information thats is not being used at the momment.
  99.    // public uiManager uimanager;
  100.     public GameObject crossHair;
  101.    
  102.     /// <summary>
  103.     /// all of my public variables used in this script
  104.     ///
  105.     /// </summary>
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.     private void Start()
  114.     {
  115.        
  116.  
  117.        // uimanager.refreshAmmo(currentAmmo);
  118.         audioSource = GetComponent<AudioSource>();
  119.  
  120.         // Start edits //
  121.         if (currentAmmo <= 0 && maxNumberOfClips > 0)
  122.         {
  123.             currentClips = maxNumberOfClips;
  124.             currentAmmo = maxAmmo;
  125.  
  126.             // If you want to take a clip away at start (ie. player has just loaded one in)
  127.             // uncomment the thing below
  128.              currentClips -= 1;
  129.         }
  130.         // End edits //
  131.     }
  132.     public void AddRecoil()
  133.     {
  134.         recoil += 0.01f;
  135.     }
  136.  
  137.     void Recoil() // this is the function to add recoil to the weapon " gun "
  138.     {
  139.         if (recoil > 0)
  140.         {
  141.             var maxRecoil = Quaternion.Euler(maxRecoilX + Random.Range(-10, 10), maxRecoilY + Random.Range(-10, 10), 0);
  142.             transform.localRotation = Quaternion.Slerp(transform.localRotation, maxRecoil, Time.deltaTime * recoilSpeed); // This controls the actual recoil
  143.             transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z); // This one also
  144.             recoil -= Time.deltaTime;
  145.         }
  146.         else
  147.         {
  148.             recoil = 0;
  149.             var minRecoil = Quaternion.Euler(0, 0, 0);
  150.             transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, Time.deltaTime * recoilSpeed / 2);
  151.             transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
  152.  
  153.         }
  154.     }
  155.  
  156.  
  157. private void OnEnable() // private function for calling the reloading
  158.     {
  159.         isReloading = false;
  160.         animator.SetBool("Reloading", false);
  161.     }
  162.  
  163.     // Update is called once per frame
  164.  
  165.     void Update()
  166.     {
  167.  
  168.         /*if (Input.GetKeyDown("e"))
  169.         {
  170.             wepaonInHand.SetActive(false);
  171.             Instantiate(pickUpWeapon, transform.position, transform.rotation);
  172.         }*/
  173.        
  174.         Recoil();
  175.  
  176.         // uimanager.refreshAmmo(maxNumberOfClips);
  177.  
  178.         if (isReloading)
  179.             return;
  180.  
  181.         if (currentAmmo <= 0)
  182.         {
  183.             StartCoroutine(Reload());
  184.             return;
  185.  
  186.            
  187.         }
  188.  
  189.         // Start edits //
  190.         if (currentAmmo > 0 && Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
  191.            
  192.         {
  193.             nextTimeToFire = Time.time + 1f / fireRate;
  194.             Shoot();
  195.             Recoil();
  196.         }
  197.  
  198.     {
  199.         float tiltX = Input.GetAxis("Mouse X") * tiltAngle;
  200.         float tiltY = Input.GetAxis("Mouse Y") * tiltAngle;
  201.         Quaternion target = Quaternion.Euler(tiltY, tiltX, 0);
  202.         transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smoothComplexity);
  203.     }
  204.  
  205.         //
  206.        // mouseX = Input.GetAxis("Mouse X");
  207.         //mouseY = Input.GetAxis("Mouse Y");
  208.         //rotationSpeed = Quaternion.Euler(-mouseY, mouseX, 0);
  209.         //transform.localRotation = Quaternion.Slerp(transform.localRotation, rotationSpeed, speed * Time.deltaTime);
  210.         // End edits //
  211.  
  212.        if (Input.GetButtonDown("Fire2"))
  213.         {
  214.             transform.localPosition = aimDownSight;
  215.             crossHair.SetActive(false);
  216.             normalFOV = mainCamera.fieldOfView;
  217.             mainCamera.fieldOfView = scopedFOV;
  218.         }
  219.  
  220.         if (Input.GetButtonUp("Fire2"))
  221.         {
  222.             transform.localPosition = hipFire;
  223.             crossHair.SetActive(true);
  224.             mainCamera.fieldOfView = normalFOV;
  225.         }
  226.     }
  227.  
  228.     IEnumerator Reload()
  229.     {
  230.         Debug.Log("Reloading....");
  231.  
  232.         // Start edits //
  233.         if (currentClips == 0)
  234.         {
  235.             // No clips? Then no reload for you.
  236.             noMoreBullets();
  237.             Debug.Log("Reload failed; no clips!");
  238.         }
  239.         else
  240.         {
  241.             isReloading = true;
  242.  
  243.             currentClips -= 1;    // Take a clip away
  244.             yield return new WaitForSeconds(reloadTime);
  245.  
  246.             animator.SetBool("Reloading", false);
  247.  
  248.             currentAmmo = maxAmmo;
  249.             isReloading = false;
  250.         }
  251.         // End edits //
  252.  
  253.     }
  254.  
  255.     // Why is this public? //
  256.     void noMoreBullets()
  257.     {
  258.         if (Input.GetButtonDown("Fire1"))
  259.         {
  260.             audioSource.PlayOneShot(gunShotEmpty, 0.7F);
  261.         }
  262.        
  263.         return;
  264.     }
  265.  
  266.  
  267.     void impactParticleSystems(GameObject particlesystem)
  268.     {
  269.  
  270.     }
  271.  
  272.     void Shoot()
  273.     {
  274.  
  275.         muzzleFlash.Play();
  276.         AddRecoil();
  277.         RaycastHit hit;
  278.         currentAmmo--;
  279.  
  280.         // UI refresh
  281.        // uimanager.refreshAmmo(currentAmmo);
  282.  
  283.         GameObject shellCasings = Instantiate(shellCasing, bulletEject.position, bulletEject.rotation);
  284.         shellCasings.GetComponent<Rigidbody>().AddForce(bulletEject.right * myforce);
  285.         Destroy(shellCasings, 2f);
  286.  
  287.         audioSource.PlayOneShot(gunShotFire, 0.7F);
  288.  
  289.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
  290.         {
  291.             Debug.Log(hit.transform.name);
  292.             switch (hit.collider.tag)
  293.             {
  294.                 case "flesh":
  295.                     impactGO = impactPrefabflesh;
  296.                     break;
  297.                 case "sand":
  298.                     impactGO = impactPrefabsand;
  299.                     break;
  300.                 case "wood":
  301.                     impactGO = impactPrefabwood;
  302.                     break;
  303.                 case "metal":
  304.                     impactGO = impactPrefabmetal;
  305.                     break;
  306.                 case "stone":
  307.                     impactGO = impactPrefabstone;
  308.                     break;
  309.                 case "water":
  310.                      impactGO = impactPrefabwater;
  311.                     break;
  312.                 default:
  313.                     impactGO = impactPrefab;
  314.                     break;
  315.             }
  316.  
  317.  
  318.  
  319.             Destructiable target = hit.transform.GetComponent<Destructiable>();
  320.             if (target != null)
  321.             {
  322.                 target.TakeDamage(damage);
  323.             }
  324.  
  325.             if (hit.rigidbody != null)
  326.             {
  327.                 hit.rigidbody.AddForce(hit.normal * impactForce);
  328.             }
  329.  
  330.             GameObject impactgo = Instantiate(impactGO, hit.point, Quaternion.LookRotation(hit.normal));
  331.             Destroy(impactgo, 2f);
  332.  
  333.         }
  334.  
  335.  
  336.     }
  337.  
  338.  
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement