TeHArGiS10

Untitled

Aug 11th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Shooting : MonoBehaviour {
  6.  
  7. //All
  8. public GameObject laserBeam;
  9. public float shootingForce;
  10. public bool inDelay;
  11. public float shootingDelay = 1f;
  12. public int healthCount = 100;
  13. public GameObject mainUFO;
  14.  
  15. //Arrays
  16. public GameObject[] enemyUfos;
  17. public List<GameObject> newUfos = new List<GameObject>();
  18. public GameObject[] spawnPoints;
  19.  
  20. //Components
  21. Rigidbody rb;
  22. RaycastHit hit;
  23. AudioSource audioSource;
  24.  
  25. //Array 0 = Mave
  26. //Array 1 = MillenniumFalcon
  27.  
  28. void Start ()
  29. {
  30. InvokeRepeating("newEnemySpawn", 2, 8f);
  31.  
  32. inDelay = false;
  33. audioSource = GetComponent<AudioSource>();
  34. }
  35.  
  36. void newEnemySpawn()
  37. {
  38. int randomInt = Random.Range(0, 19);
  39.  
  40. GameObject newUfo = Instantiate(enemyUfos[Random.Range(0, 2)], new Vector3(spawnPoints[randomInt].transform.position.x, spawnPoints[randomInt].transform.position.y, spawnPoints[randomInt].transform.position.z), Quaternion.identity) as GameObject;
  41. newUfos.Add(newUfo);
  42. }
  43.  
  44. void Update ()
  45. {
  46. for (int i = 0; i < newUfos.Count; i++)
  47. {
  48. Transform getChild = newUfos[i].gameObject.transform.GetChild(0);
  49. getChild.transform.rotation = Quaternion.LookRotation(getChild.transform.position - mainUFO.transform.position);
  50. }
  51.  
  52. if (Input.GetMouseButton(0))
  53. {
  54. if (!inDelay)
  55. {
  56. StartCoroutine(newShooting());
  57.  
  58. if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit))
  59. {
  60. StartCoroutine(shootingDamageDelay());
  61. }
  62. }
  63. }
  64. }
  65.  
  66. IEnumerator newShooting ()
  67. {
  68. GameObject newLaserBeam = Instantiate(laserBeam, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation) as GameObject;
  69. newLaserBeam.transform.Translate(Vector3.forward * Time.deltaTime, Space.World);
  70.  
  71. rb = newLaserBeam.GetComponent<Rigidbody>();
  72. rb.AddForce(transform.forward * shootingForce);
  73.  
  74. audioSource.Play();
  75.  
  76. inDelay = true;
  77. yield return new WaitForSeconds(shootingDelay);
  78. inDelay = false;
  79.  
  80. yield return new WaitForSeconds(1);
  81. Object.Destroy(newLaserBeam);
  82. }
  83.  
  84. IEnumerator shootingDamageDelay ()
  85. {
  86. yield return new WaitForSeconds(0.2f);
  87. hit.transform.SendMessage("recDamage");
  88. }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment