Advertisement
Transformator

Gun_Controller

Dec 21st, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  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. var weaponTimeout = 0;
  19. // for halfautomatic weapons "1" and for fullautomatic weapons set to "0".
  20. var singleshoot : int = 1;
  21. // List with all bullets
  22. var bulletList = ArrayList();
  23.  
  24. // Startfunktion executed if the objekt is created.
  25. function Start () {
  26. // Resets the Timeout (only because of error avoiding).
  27. weaponTimeout = 0;
  28. }
  29.  
  30. // Updatefunktion executed every new Frame.
  31. function Update () {
  32. //if weapon ready to shoot (Timeout = 0).
  33. if (weaponTimeout == 0) {
  34. // if it's an automatic gun.
  35. if (singleshoot == 0) {
  36. // if Mouse Button is pressed down.
  37. if (Input.GetMouseButton(0)) {
  38. // creates the Bullet (it automaticly goes to spawnposition).
  39. fire();
  40. weaponTimeout = maxWeaponTimeout;
  41. }
  42. // if it isn't a automatic gun
  43. } else {
  44. // if this Frame the Button gets pressed down.
  45. if (Input.GetMouseButtonDown(0)) {
  46. // creates the Bullet (it automaticly goes to spawnposition).
  47. Instantiate(bullet);
  48. // sets the WeaponTimeout to maximum.
  49. weaponTimeout = maxWeaponTimeout;
  50. }
  51. }
  52. // if the weaponTimeout isn't 0
  53. } else {
  54. // counts weaponTimeout 1 down
  55. weaponTimeout--;
  56. // if weaponTimeout is negative
  57. if (weaponTimeout <= -1) {
  58. // Throws Exception
  59. Debug.Log("weaponTimeout is negative");
  60. // Ends the application
  61. Application.Quit();
  62. }
  63. }
  64. }
  65.  
  66. // This funktion fires the bullet and sets it's roation, scale and position.
  67. function fire()
  68. {
  69. // creates a reference to bullet
  70. var b = Instantiate(bullet);
  71.  
  72. // sets position of bullet to spawnposition
  73. b.transform.position.x = transform.position.x;
  74. b.transform.position.y = transform.position.y;
  75. b.transform.position.z = transform.position.z;
  76.  
  77. // sets rotation of bullet to spawnposition
  78. b.transform.rotation.x = transform.rotation.x;
  79. b.transform.rotation.y = transform.rotation.y;
  80. b.transform.rotation.z = transform.rotation.z;
  81.  
  82. // adds bullet to bulletList
  83. bulletList.Add(b);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement