Advertisement
Guest User

Unity for noobs Shooting version 1.5

a guest
May 29th, 2015
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2. //verison 1.5
  3. //NEW FEATURES
  4. //portal gun support
  5. //the point at which we want to fire the bullet from
  6. var FiringPoint:Transform;
  7. //the bullet itself
  8. var Bullet:Transform;
  9. //the point we want the bullet to travel to
  10. var TargetPoint:Vector3;
  11. //the person this gun is connented to
  12. var Player:Healthbar;
  13. // is this person online
  14. var Online:boolean;
  15. //is this person offline
  16. var Offline:boolean;
  17. //this gets the stats for the weapon we are using
  18. var weapon:WeaponClass;
  19. //did we shoot the first side of the portal
  20. var FirstPortal:boolean;
  21. //the begin side of the portal we want to create
  22. var BeginPortal:Transform;
  23. //the end side of the portal we want to shoot
  24. var EndPoint:Transform;
  25.  
  26.  
  27.  
  28. //this is here to avoid errors by restarting the script
  29. function InitzializeShooting(){
  30.  yield WaitForSeconds(0.1);
  31.  this.enabled=false;
  32.  yield WaitForSeconds(0.1);
  33.  this.enabled=true;
  34. }
  35.  
  36. function Start(){
  37.  InitzializeShooting();
  38.  //lets the script know if we are online or not
  39.  Player=GetComponentInParent(Healthbar);
  40.  if(Network.isClient||Network.isServer){
  41.  Online=true;
  42.  if(Player.GetComponent(NetworkView).isMine){
  43.  
  44.  }else{
  45.  //if we are online this turn the scripts we dont need off
  46.  //this is so we dont controller other peoples cameras and stuff
  47.  transform.GetComponent(AudioListener).enabled=false;
  48.  transform.GetComponent(Camera).enabled=false;
  49.  transform.GetComponent(MouseLook).enabled=false;
  50.  }
  51.  }else{
  52.  Offline=true;
  53.  }
  54. }
  55.  
  56.  
  57.  
  58. function Update(){
  59.    
  60.         var beam : Ray = transform.GetComponent.<Camera>().ViewportPointToRay (Vector3(0.5,0.5,0));
  61.         var hit : RaycastHit;
  62.         //if you press the first mouse key it makes the bullet and tells the bullet its target
  63.         if(Player.GetComponent(NetworkView).isMine||Offline){
  64.             //checks if we have ammo if we do it lets us shoot
  65.             if(Input.GetKeyDown(KeyCode.Mouse0)){
  66.                 if(transform.GetComponentInParent(CharacterInventory).ammo>0){
  67.  
  68.                     weapon=transform.GetComponentInChildren(WeaponClass);
  69.                     transform.GetComponentInParent(CharacterInventory).ammo-=1;
  70.                     //the beam comes from the camera itself to the tagetpoint
  71.                     if(weapon){
  72.                     //fires the gun from with the bullet starting at the firing point and ending up at the targetpoint
  73.                     var layerMask = 1 << 8;
  74.                     layerMask = ~layerMask;
  75.                     if (Physics.Raycast (beam, hit,Mathf.Infinity,layerMask)){
  76.                         //tells the beam this is the target point
  77.                         TargetPoint=hit.point;
  78.                         //draws the beam in the senceview so you can see it
  79.                         Debug.DrawLine (beam.origin, hit.point);      
  80.                     }
  81.                     if(Player.GetComponent(CharacterInventory)&&!Player.GetComponent(CharacterInventory).ShowInventory||!Player.GetComponent(CharacterInventory)){
  82.                     //fire on offline and and online bullet
  83.                     //depending on weather you are offline or online
  84.                     //if we have the first portal and the end portal
  85.                     //and shoot a bullet we want to detrory old portals
  86.                     if(BeginPortal&&EndPoint){
  87.                     Destroy (BeginPortal.gameObject);
  88.                     Destroy (EndPoint.gameObject);
  89.                     }
  90.                     var Gunshot : Transform;
  91.                     var tempGunDamage:float=Random.Range(weapon.Damage,weapon.Damage-5);
  92.                     Gunshot=Instantiate(Bullet,FiringPoint.position,transform.rotation);
  93.                     //damage is now more realist it does a ramdom between damage and damage +5
  94.                     //now allows damage from diffent guns online
  95.                     Gunshot.GetComponent(Bullet_Controller).Damage=tempGunDamage;
  96.                     Gunshot.GetComponent(Bullet_Controller).Point=TargetPoint;
  97.                     Gunshot.GetComponent(Bullet_Controller).Player=Player.transform;
  98.                     //calls the online shooting function
  99.                     //i change the way bullets work to stops errors
  100.                     if(Online){
  101.                         transform.GetComponent(NetworkView).RPC("OnlineGunshot",RPCMode.OthersBuffered,tempGunDamage,TargetPoint.x,TargetPoint.y,TargetPoint.z);
  102.                     }
  103.                     }
  104.                    
  105.                 }
  106.             }
  107.         }
  108.         //resets the target point in an effort to avoid errors
  109.         TargetPoint=new Vector3(0,0,0);
  110.     }
  111. }
  112.  
  113.  
  114. //the new function to create a bullet online
  115. //now bullets are created locally to stoperrors
  116. //this funtion gets the damage and target point
  117. //then creates a bullet on other people computers
  118. @RPC
  119. function OnlineGunshot(dam:float,x:float,y:float,z:float){
  120.  var GunshotOnline : Transform;
  121.  GunshotOnline=Instantiate(Bullet,FiringPoint.position,transform.rotation);
  122.  GunshotOnline.GetComponent(Bullet_Controller).Damage=dam;
  123.  GunshotOnline.GetComponent(Bullet_Controller).Point=Vector3(x,y,z);
  124.  GunshotOnline.GetComponent(Bullet_Controller).Player=Player.transform;
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement