Advertisement
Guest User

Untitled

a guest
May 30th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. public class PlayerGuns : MonoBehaviour
  6. {
  7. //current bugs, when you click q when number of guns equals either 1 or 3 its still destroys guns
  8. //list of numbers of guns
  9. public int[] guns;
  10.  
  11. public AudioClip ColtShoot;
  12. public AudioClip ColtShootSecond;
  13. public int ChooseColt = 0;
  14.  
  15. private AudioSource source;
  16.  
  17. public float VolumeLowRange;
  18. public float VolumeHighRange;
  19.  
  20.  
  21. //gameobject guns, to be added
  22. GameObject[] gunsobj;
  23.  
  24. //checks what 1 2 3 was pressed
  25. public float numberofguns;
  26.  
  27.  
  28. public float primaryammo;
  29. public float secondaryammo;
  30.  
  31. //delay between shots for colt
  32. public float coltbetweenshots;
  33. //how long it takes to reload with colt
  34. public float reloadcolttime;
  35. //how big a colt clip is
  36. public float coltclip;
  37. //the damage the colt inflicts to enemy
  38. public float coltdamage;
  39. //the colt has not fired at start
  40. private bool firedcolt = false;
  41. //range of colt
  42. public float rangeofcolt;
  43. //how much reloadcolt
  44. public float reloadcolt = 8;
  45. private bool dontreloadeveryupdatecolt = false;
  46. public bool reloadedcolt = false;
  47.  
  48. //time it takes to swing hammer
  49. public float hammerbetweenshots;
  50. //the damage the hammer deals
  51. public float hammerdamage;
  52. //the hammer has not fired at start
  53. public bool firedhammer = false;
  54.  
  55. //check if the player collided with colt
  56. public bool collidedwithcolt;
  57. public bool collidedwithhammer;
  58. //check if the colt was already created
  59. public bool createdguncolt;
  60. public bool createdhammer;
  61. //just a double check for the colt
  62. public bool doublecheckcolt;
  63.  
  64. //fake colt gameobject
  65. private GameObject fakecolt;
  66.  
  67. private GameObject fakehammer;
  68.  
  69.  
  70.  
  71. //player gameobject
  72. public GameObject player;
  73. //hand gameobject or what the gun goes to when it was created
  74. public GameObject hand;
  75. //were the coltprefab spawns when the player clicks q
  76. public GameObject dropit;
  77. //the fakecolt prefab that is in the players hand
  78. public GameObject fakecoltprefab;
  79. //the fakehammerofdeath prefab that is in the players hand
  80. public GameObject fakehammerprefab;
  81. //the colt that is in the ground and not in the players hand
  82. public GameObject coltprefab;
  83. //the hammerofdeath that is in the ground and not in the players hand
  84. public GameObject hammerprefab;
  85. //camera
  86. public GameObject hidingcamera;
  87.  
  88. public Transform barrel;
  89.  
  90.  
  91. //public LayerMask layermask = -1;
  92.  
  93.  
  94.  
  95. void Awake ()
  96. {
  97. source = GetComponent <AudioSource>();
  98. }
  99.  
  100. void Start ()
  101. {
  102. numberofguns = 1;
  103. //find player
  104. player = GameObject.Find("Player");
  105. //gun named colt has never been created
  106. createdguncolt = true;
  107. createdhammer = true;
  108. }
  109.  
  110. //if you collide guns, if you wanna create new ones just copy and paste with different tag
  111. void OnTriggerEnter(Collider other)
  112. {
  113. //if you collide with a gameobject with the tag "Colt"
  114. //If key E is down
  115. //guns[2] == 1 is the type of gun 2 as hand and 1 as the type of gun in hand
  116. if(other.gameObject.tag == "Colt" && (Input.GetKey(KeyCode.E) && !(guns[2] == 1)))
  117. {
  118. guns[2] = 1;
  119. collidedwithcolt = true;
  120. //player has collided with colt
  121. fakecolt = Instantiate(fakecoltprefab) as GameObject;
  122. fakecolt.transform.position = hand.transform.position;
  123. fakecolt.transform.parent = hand.transform;
  124. fakecolt.transform.rotation = player.transform.rotation;
  125. //create a fake colt in the players general direction
  126. Destroy (other.gameObject);
  127.  
  128. //destorying the fake object
  129. Debug.Log("I have picked up a gun with the tag colt and pressed E");
  130. }
  131. else
  132. {
  133. collidedwithcolt = false;
  134. }
  135. if(other.gameObject.tag == "hammerofdeath" && (Input.GetKey(KeyCode.E) && !(guns[3] == 1)))
  136. {
  137. Debug.Log("Collided with hammer");
  138. guns[3] = 1;
  139. collidedwithhammer = true;
  140. //player has collided with colt
  141. fakehammer = Instantiate(fakehammerprefab) as GameObject;
  142. fakehammer.transform.position = hand.transform.position;
  143. fakehammer.transform.parent = hand.transform;
  144. fakehammer.transform.rotation = player.transform.rotation;
  145. //create a fake colt in the players general direction
  146. Destroy (other.gameObject);
  147.  
  148. //destorying the fake object
  149. Debug.Log("I have picked up a gun with the tag hammer and pressed E");
  150. }
  151. else
  152. {
  153. collidedwithhammer = false;
  154. }
  155. //double checking if the colt is in the players hand
  156. if(other.gameObject.tag == "fakecolt")
  157. {
  158. doublecheckcolt = true;;
  159. }
  160. else
  161. {
  162. doublecheckcolt = false;
  163. }
  164.  
  165. }
  166. IEnumerator FireColt ()
  167. {
  168. RaycastHit hit;
  169. Ray ray = new Ray(barrel.position, transform.forward);
  170.  
  171. if(Physics.Raycast(ray, out hit, rangeofcolt))
  172. {
  173. if(hit.collider.tag == "enemy")
  174. {
  175. Enemy enemy = hit.collider.GetComponent<Enemy>();
  176. Debug.Log("getting enemy info");
  177. enemy.enemyhealth -= coltdamage;
  178. }
  179. }
  180.  
  181. Debug.DrawRay(fakecolt.transform.position, transform.forward * rangeofcolt, Color.green);
  182. //yield return null;
  183. yield return new WaitForSeconds(coltbetweenshots);
  184. firedcolt = false;
  185. }
  186. IEnumerator FireHammer ()
  187. {
  188. RaycastHit hit;
  189. Ray hammerray = new Ray(barrel.position, transform.forward);
  190. if(Physics.Raycast(hammerray, out hit, rangeofcolt))
  191. {
  192. if(hit.collider.tag == "enemy")
  193. {
  194. Enemy enemy = hit.collider.GetComponent<Enemy>();
  195. Debug.Log("getting enemy info");
  196. enemy.enemyhealth -= hammerdamage;
  197. }
  198. }
  199. Debug.DrawRay(fakehammer.transform.position, transform.forward * rangeofcolt, Color.green);
  200. // yield return null;
  201. yield return new WaitForSeconds(hammerbetweenshots);
  202. firedhammer = false;
  203. }
  204. IEnumerator ReloadColt()
  205. {
  206. if (coltclip <= 0 && secondaryammo >= 8)
  207. {
  208. yield return new WaitForSeconds(reloadcolttime);
  209. coltclip += reloadcolt;
  210. secondaryammo -= reloadcolt;
  211. reloadedcolt = false;
  212. }
  213. }
  214. void Update ()
  215. {
  216. if (coltclip > 8)
  217. {
  218. coltclip = 8;
  219. }
  220. if (coltclip <= 0 && secondaryammo >= 8 && Input.GetKeyDown(KeyCode.R) && reloadedcolt == false)
  221. {
  222. reloadedcolt = true;
  223. StartCoroutine(ReloadColt());
  224. }
  225. //if ((Input.GetKey(KeyCode.Mouse1) && numberofguns == 2 && guns[2] == 1))
  226. // {
  227. // RaycastHit hit;
  228. // if (Physics.Raycast(transform.position,transform.forward, out hit, Mathf.Infinity, layermask))
  229. // {
  230. // hit.collider.SendMessage("Damage", damage,SendMessageOptions.DontRequireReceiver);
  231. // }
  232. //}
  233. // guns[2] == 1 is the type of gun 2 as hand and 1 as type of gun in hand
  234. // number of guns is which hand is held
  235. // key q is down to drop gun
  236. if ((Input.GetKey(KeyCode.Mouse0) && guns[2] == 1 && numberofguns == 2 && !firedcolt && coltclip > 0))
  237. {
  238. coltclip -= 1;
  239. firedcolt = true;
  240. Debug.Log("Starting fire");
  241. StartCoroutine("FireColt");
  242. ChooseColt = Random.Range (1,2);
  243. if (ChooseColt == 1)
  244. {
  245. float vol = Random.Range (VolumeLowRange, VolumeHighRange);
  246. source.PlayOneShot(ColtShoot, vol);
  247. Debug.Log ("Colt shoot");
  248. }
  249. if (ChooseColt == 2)
  250. {
  251. float vol = Random.Range (VolumeLowRange, VolumeHighRange);
  252. source.PlayOneShot(ColtShootSecond, vol);
  253. Debug.Log ("Colt Shoot 2nd");
  254. }
  255. }
  256. if ((Input.GetKey(KeyCode.Mouse0) && guns[3] == 1 && numberofguns == 3 && !firedhammer))
  257. {
  258. coltclip -= 1;
  259. firedhammer = true;
  260. Debug.Log("Starting fire");
  261. StartCoroutine("FireHammer");
  262. }
  263. if ((Input.GetKey(KeyCode.Q) && guns[2] == 1 && numberofguns == 2))
  264. {
  265. Debug.Log("I pressed q and will drop gun");
  266. guns[2] = 0;
  267. GameObject colt = Instantiate(coltprefab) as GameObject;
  268. colt.transform.position = dropit.transform.position;
  269. //Destroy(fakecolt);
  270. }
  271. if ((Input.GetKey(KeyCode.Q) && guns[3] == 1 && numberofguns == 3))
  272. {
  273. Debug.Log("I pressed q and will drop hammer");
  274. guns[3] = 0;
  275. GameObject hammer = Instantiate(hammerprefab) as GameObject;
  276. hammer.transform.position = dropit.transform.position;
  277. //Destroy(fakecolt);
  278. }
  279. if (guns[2] == 1 && numberofguns == 1)
  280. {
  281. //make it so you cant see the colt
  282. fakecolt.SetActive(false);
  283.  
  284. }
  285. // if the gun colt in the array is active and number 3 is active
  286. if (guns[2] == 1 && numberofguns == 3)
  287. {
  288. //make it so you cant see the colt
  289. fakecolt.SetActive(false);
  290. }
  291. // if the gun colt in the array is active and number 2 is active
  292. if (guns[2] == 1 && numberofguns == 2)
  293. {
  294. //make it so you can see the colt
  295. fakecolt.SetActive(true);
  296. }
  297. //hammertime
  298. if (guns[3] == 1 && numberofguns == 1)
  299. {
  300.  
  301. fakehammer.SetActive(false);
  302. }
  303. if (guns[3] == 1 && numberofguns == 2)
  304. {
  305.  
  306. fakehammer.SetActive(false);
  307. }
  308. if (guns[3] == 1 && numberofguns == 3)
  309. {
  310.  
  311. fakehammer.SetActive(true);
  312. }
  313.  
  314.  
  315.  
  316.  
  317.  
  318. //checks if you are holding 2nd hand
  319.  
  320. /*
  321. else
  322. {
  323. Destroy (fakecoltprefab);
  324. }
  325. */
  326.  
  327.  
  328. //set number of gun
  329. if (Input.GetKey(KeyCode.Alpha1))
  330. {
  331. //key 1 pressed
  332. numberofguns = 1;
  333. }
  334. if (Input.GetKey(KeyCode.Alpha2))
  335. {
  336. //key 2 pressed
  337. numberofguns = 2;
  338. }
  339. if (Input.GetKey(KeyCode.Alpha3))
  340. {
  341. //key 3 pressed
  342. numberofguns = 3;
  343. }
  344. }
  345.  
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement