Advertisement
Guest User

unityshot

a guest
Jan 21st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class code_PlayerShoot : MonoBehaviour {
  6. public GameObject player;
  7. public GameObject playerBullet;
  8. public GameObject katanaObject;
  9. private float bulletSpeed = 20;
  10. private bool reloading = false;
  11. private float reloadTimer;
  12. private float reloadDefault;
  13. private int weapon = 0;
  14. private int ammo; // implement ammo when come back
  15. private bool weaponAuto;
  16. private bool meleeMode;
  17.  
  18. void Start()
  19. {
  20. meleeMode = true;
  21. weaponAuto = false;
  22. changeWeapon(0);
  23. reloadTimer = reloadDefault;
  24. }
  25.  
  26. void Update () {
  27.  
  28. // Test key pad weapon change
  29. if (Input.GetKeyDown(KeyCode.Keypad1)) changeWeapon(0);
  30. if (Input.GetKeyDown(KeyCode.Keypad2)) changeWeapon(1);
  31. if (Input.GetKeyDown(KeyCode.Keypad3)) changeWeapon(2);
  32. if (Input.GetKeyDown(KeyCode.Keypad4)) changeWeapon(3);
  33.  
  34. if (meleeMode)
  35. {
  36. katanaSliceAttack();
  37. }
  38. else if (weaponAuto)
  39. {
  40. gunAutoShoot();
  41. }
  42. else if (!weaponAuto)
  43. {
  44. gunShoot();
  45. }
  46.  
  47. gunReload();
  48. }
  49.  
  50. // Consumes ammo
  51. void consumeAmmo()
  52. {
  53. if (weapon != 0 && ammo > 0)
  54. {
  55. ammo--;
  56. Debug.Log("Ammo: " + ammo);
  57. }
  58.  
  59. if(ammo == 0)
  60. {
  61. changeWeapon(0);
  62. }
  63. }
  64.  
  65. // Changes weapon on power up
  66. void changeWeapon(int type)
  67. {
  68. weapon = type;
  69.  
  70. // Katana [0]
  71. if (type == 0)
  72. {
  73. meleeMode = true;
  74. Debug.Log("Changed to Katana!");
  75. reloadDefault = 0.46f;
  76. }
  77.  
  78. // Pistol [1]
  79. if (type == 1)
  80. {
  81. Debug.Log("Changed to Pistol!");
  82. ammo = 24;
  83. bulletSpeed = 21.7f;
  84. weaponAuto = false;
  85. reloadDefault = 0.41f;
  86. }
  87.  
  88. // AC470 [2]
  89. if (type == 2)
  90. {
  91. Debug.Log("Changed to AK!");
  92. ammo = 47;
  93. bulletSpeed = 47.71f;
  94. weaponAuto = true;
  95. reloadDefault = 0.16f;
  96. }
  97.  
  98. // Shotgun [3]
  99. if (type == 3)
  100. {
  101. Debug.Log("Changed to Shotgun!");
  102. ammo = 18;
  103. bulletSpeed = 27.6f;
  104. weaponAuto = false;
  105. reloadDefault = 0.46f;
  106. }
  107.  
  108. reloadTimer = reloadDefault;
  109. if (type != 0) meleeMode = false;
  110. }
  111.  
  112. // Non-auto shooting (spawns player bullet)
  113. void gunShoot()
  114. {
  115. if (Input.GetMouseButtonDown(0) && !reloading)
  116. {
  117. // Finds mouse position and sets bullet direction
  118. Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  119. Vector2 playerPos = new Vector2(player.transform.position.x, player.transform.position.y);
  120. Vector2 direction = target - playerPos;
  121. direction.Normalize();
  122.  
  123. GameObject projectile = (GameObject)Instantiate(playerBullet, player.transform.position, Quaternion.identity);
  124. projectile.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
  125.  
  126. // Shotgun two additional side bullets
  127. if(weapon == 3)
  128. {
  129. direction.y += 0.12f;
  130. direction.Normalize();
  131. GameObject projectile1 = (GameObject)Instantiate(playerBullet, player.transform.position, Quaternion.identity);
  132. projectile1.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
  133.  
  134. direction.y -= 0.24f;
  135. direction.Normalize();
  136. GameObject projectile2 = (GameObject)Instantiate(playerBullet, player.transform.position, Quaternion.identity);
  137. projectile2.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
  138. }
  139.  
  140. consumeAmmo();
  141. reloading = true;
  142. }
  143. }
  144.  
  145. // Auto shooting (spawns player bullet)
  146. void gunAutoShoot()
  147. {
  148. if (Input.GetMouseButton(0) && !reloading)
  149. {
  150. Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  151. Vector2 playerPos = new Vector2(player.transform.position.x, player.transform.position.y);
  152. Vector2 direction = target - playerPos;
  153. direction.Normalize();
  154.  
  155. GameObject projectile = (GameObject)Instantiate(playerBullet, player.transform.position, Quaternion.identity);
  156. projectile.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
  157.  
  158. consumeAmmo();
  159. reloading = true;
  160. }
  161. }
  162.  
  163. void katanaSliceAttack()
  164. {
  165. if (Input.GetMouseButtonDown(0) && !reloading && player != null)
  166. {
  167. Instantiate(katanaObject,
  168. new Vector2(player.transform.position.x + 1.62f, player.transform.position.y - 0.61f),
  169. Quaternion.identity);
  170.  
  171. reloading = true;
  172. }
  173.  
  174. }
  175.  
  176. // Delay before player can shoot again
  177. void gunReload()
  178. {
  179. if (reloading)
  180. {
  181. reloadTimer -= Time.deltaTime;
  182. if (reloadTimer <= 0)
  183. {
  184. reloading = false;
  185. reloadTimer = reloadDefault;
  186. }
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement