Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma strict
- //verison 1.5
- //NEW FEATURES
- //portal gun support
- //the point at which we want to fire the bullet from
- var FiringPoint:Transform;
- //the bullet itself
- var Bullet:Transform;
- //the point we want the bullet to travel to
- var TargetPoint:Vector3;
- //the person this gun is connented to
- var Player:Healthbar;
- // is this person online
- var Online:boolean;
- //is this person offline
- var Offline:boolean;
- //this gets the stats for the weapon we are using
- var weapon:WeaponClass;
- //did we shoot the first side of the portal
- var FirstPortal:boolean;
- //the begin side of the portal we want to create
- var BeginPortal:Transform;
- //the end side of the portal we want to shoot
- var EndPoint:Transform;
- //this is here to avoid errors by restarting the script
- function InitzializeShooting(){
- yield WaitForSeconds(0.1);
- this.enabled=false;
- yield WaitForSeconds(0.1);
- this.enabled=true;
- }
- function Start(){
- InitzializeShooting();
- //lets the script know if we are online or not
- Player=GetComponentInParent(Healthbar);
- if(Network.isClient||Network.isServer){
- Online=true;
- if(Player.GetComponent(NetworkView).isMine){
- }else{
- //if we are online this turn the scripts we dont need off
- //this is so we dont controller other peoples cameras and stuff
- transform.GetComponent(AudioListener).enabled=false;
- transform.GetComponent(Camera).enabled=false;
- transform.GetComponent(MouseLook).enabled=false;
- }
- }else{
- Offline=true;
- }
- }
- function Update(){
- var beam : Ray = transform.GetComponent.<Camera>().ViewportPointToRay (Vector3(0.5,0.5,0));
- var hit : RaycastHit;
- //if you press the first mouse key it makes the bullet and tells the bullet its target
- if(Player.GetComponent(NetworkView).isMine||Offline){
- //checks if we have ammo if we do it lets us shoot
- if(Input.GetKeyDown(KeyCode.Mouse0)){
- if(transform.GetComponentInParent(CharacterInventory).ammo>0){
- weapon=transform.GetComponentInChildren(WeaponClass);
- transform.GetComponentInParent(CharacterInventory).ammo-=1;
- //the beam comes from the camera itself to the tagetpoint
- if(weapon){
- //fires the gun from with the bullet starting at the firing point and ending up at the targetpoint
- var layerMask = 1 << 8;
- layerMask = ~layerMask;
- if (Physics.Raycast (beam, hit,Mathf.Infinity,layerMask)){
- //tells the beam this is the target point
- TargetPoint=hit.point;
- //draws the beam in the senceview so you can see it
- Debug.DrawLine (beam.origin, hit.point);
- }
- if(Player.GetComponent(CharacterInventory)&&!Player.GetComponent(CharacterInventory).ShowInventory||!Player.GetComponent(CharacterInventory)){
- //fire on offline and and online bullet
- //depending on weather you are offline or online
- //if we have the first portal and the end portal
- //and shoot a bullet we want to detrory old portals
- if(BeginPortal&&EndPoint){
- Destroy (BeginPortal.gameObject);
- Destroy (EndPoint.gameObject);
- }
- var Gunshot : Transform;
- var tempGunDamage:float=Random.Range(weapon.Damage,weapon.Damage-5);
- Gunshot=Instantiate(Bullet,FiringPoint.position,transform.rotation);
- //damage is now more realist it does a ramdom between damage and damage +5
- //now allows damage from diffent guns online
- Gunshot.GetComponent(Bullet_Controller).Damage=tempGunDamage;
- Gunshot.GetComponent(Bullet_Controller).Point=TargetPoint;
- Gunshot.GetComponent(Bullet_Controller).Player=Player.transform;
- //calls the online shooting function
- //i change the way bullets work to stops errors
- if(Online){
- transform.GetComponent(NetworkView).RPC("OnlineGunshot",RPCMode.OthersBuffered,tempGunDamage,TargetPoint.x,TargetPoint.y,TargetPoint.z);
- }
- }
- }
- }
- }
- //resets the target point in an effort to avoid errors
- TargetPoint=new Vector3(0,0,0);
- }
- }
- //the new function to create a bullet online
- //now bullets are created locally to stoperrors
- //this funtion gets the damage and target point
- //then creates a bullet on other people computers
- @RPC
- function OnlineGunshot(dam:float,x:float,y:float,z:float){
- var GunshotOnline : Transform;
- GunshotOnline=Instantiate(Bullet,FiringPoint.position,transform.rotation);
- GunshotOnline.GetComponent(Bullet_Controller).Damage=dam;
- GunshotOnline.GetComponent(Bullet_Controller).Point=Vector3(x,y,z);
- GunshotOnline.GetComponent(Bullet_Controller).Player=Player.transform;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement