Advertisement
kasru

firing RPC

Mar 12th, 2013
2,366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. var shotSound : AudioClip; // drag a shot sound here, if any
  5. var bloodPrefab : GameObject; // drag the blood prefab here, if any
  6. var sparksPrefab : GameObject; // drag the sparks prefab here, if any
  7. var crosshair : Texture2D;
  8.  
  9. function Start () {
  10.  
  11. //Screen.showCursor = false;
  12.  
  13.  
  14. }
  15.  
  16. function OnGUI () {
  17.  
  18.     GUI.DrawTexture(Rect(Screen.width/2 - 35,Screen.height/2 - 33,64,64), crosshair);
  19. }
  20.  
  21. function Update () {
  22.      if(Input.GetMouseButtonDown(0)){
  23.             networkView.RPC("shoot", RPCMode.All);
  24.     }
  25.  }
  26.  
  27.  
  28.  
  29. @RPC
  30. function shoot () {
  31.     if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
  32.     var hit: RaycastHit;
  33.     if (Physics.Raycast(transform.position, transform.forward, hit))
  34. {
  35.         // prepare rotation to instantiate blood or sparks
  36.         var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
  37.         if (hit.transform.tag == "Player"){ // if enemy hit...
  38.             if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed...
  39.             //hit.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver); // and consume its health
  40.             hit.transform.networkView.RPC("ApplyDamage", RPCMode.All, 5);  
  41.         } else { // otherwise emit sparks at the hit point
  42.             if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement