Advertisement
Transformator

Gun_Controller

Dec 22nd, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2. /*
  3. * --Gun_Controller.js
  4. * @author: Paul Scharnofske
  5. * @version: 1.0.0
  6. *
  7. * This Script have to be bound to the Spawnpostion were the
  8. * Bullet should spawn. It have to have the right rotation because
  9. * the bullet takes over the same rotation and position of the
  10. * spawnpostion.
  11. */
  12.  
  13. // Reference to bullet Object.
  14. var bullet : GameObject;
  15. // Sets the maximal Timeout for weapon, default is 25.
  16. var maxWeaponTimeout : int = 25;
  17. // The Timeout (counts down).
  18. private var weaponTimeout : int;
  19. // for halfautomatic weapons "1" and for fullautomatic weapons set to "0".
  20. var singleshoot : int = 1;
  21.  
  22. // Startfunktion executed if the objekt is created.
  23. function Start () {
  24.     // Resets the Timeout (only because of error avoiding).
  25.     weaponTimeout = 0;
  26. }
  27.  
  28. // Updatefunktion executed every new Frame.
  29. function Update () {
  30.     //if weapon ready to shoot (Timeout = 0).
  31.     if (weaponTimeout == 0) {
  32.         // if it's an automatic gun.
  33.         if (singleshoot == 0) {
  34.             // if Mouse Button is pressed down.
  35.             if (Input.GetMouseButton(0)) {
  36.                 // creates the Bullet (it automaticly goes to spawnposition).
  37.                 fire();
  38.                 weaponTimeout = maxWeaponTimeout;
  39.             }
  40.         // if it isn't a automatic gun
  41.         } else {
  42.             // if this Frame the Button gets pressed down.
  43.             if (Input.GetMouseButtonDown(0)) {
  44.                 // creates the Bullet (it automaticly goes to spawnposition).
  45.                 Instantiate(bullet);
  46.                 // sets the WeaponTimeout to maximum.
  47.                 weaponTimeout = maxWeaponTimeout;
  48.             }
  49.         }
  50.     // if the weaponTimeout isn't 0
  51.     } else {
  52.         // counts weaponTimeout 1 down
  53.         weaponTimeout--;
  54.         // if weaponTimeout is negative
  55.         if (weaponTimeout <= -1) {
  56.             // Throws Exception
  57.             Debug.Log("weaponTimeout is negative");
  58.             // Ends the application
  59.             Application.Quit();
  60.         }
  61.     }
  62. }
  63.  
  64. // This funktion fires the bullet and sets it's roation, scale and position.
  65. function fire()
  66. {
  67.     var clone : GameObject;
  68.     clone = Instantiate(bullet, transform.position, transform.rotation);
  69.     clone.transform.position = transform.position;
  70.     clone.transform.rotation = transform.rotation;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement