333GameStudio

Updated Gun script(js)

Mar 28th, 2015
1,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1.  
  2. var BulletSpeed = 100; //force of bullets being shot
  3.  
  4. var projectile : Rigidbody; //bullet prefab goes here
  5.  
  6. var bullets = 30; //amount of bullets per "clip"
  7.  
  8. var totalBullets = 60; //total amount of bullets we have available
  9.  
  10. var reloadTime = 3; //time to reload
  11.  
  12. var reloadAmount = 10;
  13.  
  14. var ammo : GUIText; //GUI text for our bullet/ammo count
  15.  
  16. var rate : float = 0.5;
  17.  
  18. private var rate_time : float;
  19.  
  20. var Gun : GameObject;
  21.  
  22. var ammoclip : GameObject;
  23.  
  24. var flash : GameObject;
  25.  
  26. function Update () {
  27.  
  28. Fire ();
  29.  
  30. Reload ();
  31.  
  32. }
  33.  
  34. function Fire () {
  35.  
  36. if(bullets > 0) { //if we have at least 1 bullet then we can fire
  37.  
  38. if (Input.GetButtonDown("Fire1") && Time.time > rate_time) {
  39.  
  40. rate_time = Time.time + rate;
  41.  
  42. var clone : Rigidbody;// Instantiate the projectile at the position and rotation of this transform
  43.  
  44. clone = Instantiate(projectile, transform.position, transform.rotation);
  45.  
  46. clone.velocity = transform.TransformDirection (Vector3.forward * BulletSpeed);// Give the cloned object an initial velocity along the current
  47.  
  48. AudioSource.PlayClipAtPoint(sound, transform.position, 1); // object's Z axis
  49.  
  50. bullets -=1; //subtracts one bullet per fire sequence
  51.  
  52. Gun.animation.Play();
  53.  
  54. flash.animation.Play();
  55.  
  56. }
  57.  
  58. }
  59.  
  60. }
  61.  
  62. function Reload () {
  63.  
  64. if(bullets < 1) { //if we have less than 1 bullet then we can reload
  65. if (totalBullets > 0)
  66.  
  67. if(Input.GetKeyDown("r")) {
  68.  
  69. AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1); //plays reload soundclip
  70.  
  71. yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding bullets
  72.  
  73. bullets += reloadAmount; //adds 3 bullets to our "clip"
  74.  
  75. totalBullets -= reloadAmount; //subtracts 3 bullets from our totalBullets amount
  76.  
  77. ammoclip.animation.Play();
  78.  
  79. }
  80.  
  81. }
  82.  
  83. }
  84.  
  85. function OnGUI () {
  86.  
  87. ammo.text = "bullets: " + bullets + "/" + totalBullets.ToString();
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment