333GameStudio

Ammo gun script #updated

Apr 12th, 2015
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. var sound : AudioClip; //ie_shot_gun-liminalace-770179786 soundClip
  2.  
  3. var reloadSound: AudioClip; //reload soundClip
  4.  
  5. var BulletSpeed = 100; //force of bullets being shot
  6.  
  7. var projectile : Rigidbody; //bullet prefab goes here
  8.  
  9. var bullets = 30; //amount of bullets per "clip"
  10.  
  11. var totalBullets = 60; //total amount of bullets we have available
  12.  
  13. var reloadTime = 3; //time to reload
  14.  
  15. var reloadAmount = 10;
  16.  
  17. var ammo : GUIText; //GUI text for our bullet/ammo count
  18.  
  19. var rate : float = 0.5;
  20.  
  21. var Gun : GameObject;
  22.  
  23. var ammoclip : GameObject;
  24.  
  25. var flash : GameObject;
  26.  
  27. var stars : ParticleEmitter;
  28.  
  29. var planes : GameObject[];
  30.  
  31. private var randNumber: int;
  32.  
  33.  
  34.  
  35. private var rate_time : float;
  36.  
  37. function Start () {
  38. for (var i = 0; i < planes.Length; i++)
  39. planes[i].SetActive(false);
  40. }
  41.  
  42.  
  43. function Update () {
  44.  
  45. Fire ();
  46.  
  47. Reload ();
  48.  
  49. }
  50.  
  51. function Fire () {
  52.  
  53. if(bullets > 0) { //if we have at least 1 bullet then we can fire
  54.  
  55. if (Input.GetButton("Fire1") && Time.time > rate_time) {
  56.  
  57. rate_time = Time.time + rate;
  58.  
  59. var clone : Rigidbody;// Instantiate the projectile at the position and rotation of this transform
  60.  
  61. clone = Instantiate(projectile, transform.position, transform.rotation);
  62.  
  63. clone.velocity = transform.TransformDirection (Vector3.forward * BulletSpeed);
  64.  
  65.  
  66. randNumber = Random.Range(0,planes.Length);
  67.  
  68. for (var i = 0; i < planes.Length; i++)
  69. planes[i].SetActive(false);
  70.  
  71. planes[randNumber].SetActive(true);
  72.  
  73.  
  74. AudioSource.PlayClipAtPoint(sound, transform.position, 1); // object's Z axis
  75.  
  76. bullets -=1;
  77.  
  78. Gun.animation.Play();
  79.  
  80.  
  81.  
  82. }
  83.  
  84. }
  85.  
  86. }
  87.  
  88. function Reload () {
  89.  
  90. if(bullets < 1) { //if we have less than 1 bullet then we can reload
  91. if (totalBullets > 0)
  92.  
  93. if(Input.GetKeyDown("r")) {
  94.  
  95. AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1); //plays reload soundclip
  96.  
  97. yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding bullets
  98.  
  99. bullets += reloadAmount; //adds 3 bullets to our "clip"
  100.  
  101. totalBullets -= reloadAmount; //subtracts 3 bullets from our totalBullets amount
  102.  
  103. ammoclip.animation.Play();
  104.  
  105.  
  106. }
  107.  
  108. }
  109.  
  110. }
  111.  
  112. function OnGUI () {
  113.  
  114. ammo.text = "bullets: " + bullets + "/" + totalBullets.ToString();
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment