Advertisement
Guest User

GunShot

a guest
Dec 20th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2. var Range : float = 1000;
  3.  
  4. var Force : float = 1000;
  5.  
  6. static var Clips : int = 20;
  7.  
  8. var BulletPerClip : int = 32;
  9.  
  10. var ReloadTime : float = 3.3;
  11.  
  12. var Damage : int = 100;
  13.  
  14. var BulletsLeft : int = 0;
  15.  
  16. var ShootTimer : float = 0;
  17.  
  18. var ShootCooler : float = 0.9;
  19.  
  20. public var ShootAudio : AudioClip;
  21.  
  22. public var ReloadAudio : AudioClip;
  23.  
  24. function Update () {
  25.  
  26.     if(ShootTimer > 0) {
  27.    
  28.     ShootTimer -= Time.deltaTime;
  29.    
  30.     }
  31.    
  32.     if(ShootTimer < 0) {
  33.    
  34.         ShootTimer = 0;
  35.        
  36.     }
  37.  
  38.    
  39.     if(Input.GetMouseButtonDown(0) && BulletsLeft) {
  40.     if(ShootTimer == 0) {
  41.         PlayShootAudio();
  42.         RayShoot();
  43.        
  44.         ShootTimer = ShootCooler;
  45.    
  46.     }
  47.     }
  48. }
  49.  
  50. function Start() {
  51.  
  52.     BulletsLeft = BulletPerClip;
  53. }
  54.  
  55.  
  56. function RayShoot () {
  57.  
  58. //GameObject.Find("Animation Name").animation.Play("Shoot"); Needs an animation to shoot
  59.  
  60. var Hit : RaycastHit;
  61.  
  62.         var DirectionRay = transform.TransformDirection(Vector3.forward);
  63.  
  64.         Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
  65.         if(Physics.Raycast(transform.position, DirectionRay, Hit, Range)) {
  66.  
  67.         if(Hit.rigidbody) {
  68.  
  69.         Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
  70.        
  71.        
  72.         Hit.collider.SendMessageUpwards("ApplyDamage" , Damage ,SendMessageOptions.DontRequireReceiver);
  73.        
  74.         }
  75.  
  76.     }
  77.     BulletsLeft --;
  78.    
  79.     if(BulletsLeft < 0) {
  80.    
  81.     BulletsLeft = 0;
  82.    
  83.     }
  84.    
  85.     if(BulletsLeft == 0) {
  86.     Reload();
  87.    
  88.     }
  89.  
  90. }
  91.  
  92. function Reload () {
  93.         PlayReloadAudio();
  94.         //GameObject.Find("Animation Name").animation.Play("Reload"); Needs an animation to Reload
  95.     yield WaitForSeconds(ReloadTime);
  96.         if(Clips > 0) {
  97.        
  98.             BulletsLeft = BulletPerClip;
  99.             Clips -= 1;
  100.         }
  101.  
  102. }
  103.  
  104. function PlayShootAudio () {
  105.         audio.PlayOneShot( ShootAudio);
  106.  
  107. }
  108.  
  109. function PlayReloadAudio () {
  110. audio.PlayOneShot( ReloadAudio);
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement