Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Shooting : MonoBehaviour {
- //All
- public GameObject laserBeam;
- public float shootingForce;
- public bool inDelay;
- public float shootingDelay = 1f;
- public int healthCount = 100;
- public GameObject mainUFO;
- //Arrays
- public GameObject[] enemyUfos;
- public List<GameObject> newUfos = new List<GameObject>();
- public GameObject[] spawnPoints;
- //Components
- Rigidbody rb;
- RaycastHit hit;
- AudioSource audioSource;
- //Array 0 = Mave
- //Array 1 = MillenniumFalcon
- void Start ()
- {
- InvokeRepeating("newEnemySpawn", 2, 8f);
- inDelay = false;
- audioSource = GetComponent<AudioSource>();
- }
- void newEnemySpawn()
- {
- int randomInt = Random.Range(0, 19);
- 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;
- newUfos.Add(newUfo);
- }
- void Update ()
- {
- for (int i = 0; i < newUfos.Count; i++)
- {
- Transform getChild = newUfos[i].gameObject.transform.GetChild(0);
- getChild.transform.rotation = Quaternion.LookRotation(getChild.transform.position - mainUFO.transform.position);
- }
- if (Input.GetMouseButton(0))
- {
- if (!inDelay)
- {
- StartCoroutine(newShooting());
- if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit))
- {
- StartCoroutine(shootingDamageDelay());
- }
- }
- }
- }
- IEnumerator newShooting ()
- {
- GameObject newLaserBeam = Instantiate(laserBeam, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation) as GameObject;
- newLaserBeam.transform.Translate(Vector3.forward * Time.deltaTime, Space.World);
- rb = newLaserBeam.GetComponent<Rigidbody>();
- rb.AddForce(transform.forward * shootingForce);
- audioSource.Play();
- inDelay = true;
- yield return new WaitForSeconds(shootingDelay);
- inDelay = false;
- yield return new WaitForSeconds(1);
- Object.Destroy(newLaserBeam);
- }
- IEnumerator shootingDamageDelay ()
- {
- yield return new WaitForSeconds(0.2f);
- hit.transform.SendMessage("recDamage");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment