Advertisement
kasru

Firing

Mar 10th, 2013
1,774
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. function OnGUI () {
  15.  
  16.     GUI.DrawTexture(Rect(Screen.width/2,Screen.height/2,64,64), crosshair);
  17. }
  18.  
  19. function Update () {
  20.  
  21.     if(Input.GetMouseButtonDown(0)){
  22.         Shoot();
  23.         }
  24. }
  25.  
  26. function Shoot () {
  27.     if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
  28.     var hit: RaycastHit;
  29.     if (Physics.Raycast(transform.position, transform.forward, hit)){
  30.         // prepare rotation to instantiate blood or sparks
  31.         var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
  32.         if (hit.transform.tag == "Enemy"){ // if enemy hit...
  33.             if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed...
  34.             hit.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver); // and consume its health
  35.         } else { // otherwise emit sparks at the hit point
  36.             if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement