Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==Scripts Used for Enemies==//
- public class DestroyOnContact : MonoBehaviour {
- //Declare a scorevalue for the object this is attached to
- public int scoreVal;
- //Call the GameController Script
- private GameController gc;
- //When another object tagged with player collides with this, destroy both objects
- public float count;
- void Awake (){
- GameObject gco = GameObject.FindGameObjectWithTag("GameController");
- gc = gco.GetComponent<GameController>();
- count = 0;
- }
- void OnTriggerEnter2D(Collider2D other){
- if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy")) {
- return;
- }
- if (other.CompareTag ("Weapon Shot") && count < 3) {
- count = count + 1;
- Destroy (other.gameObject);
- return;
- }
- else if (count > 2) {
- Destroy (other.gameObject);
- //Rather than duplicating code here, we should write and call a function
- Destroy (gameObject);
- gc.AddScore (scoreVal);
- }
- }
- }
- //===============//
- public class Enemy1Control : MonoBehaviour {
- //What we want to do: Have the enemy move forward, only 1 direction; Have the enemy shoot; Have the enemy shoot at the player's position
- //Have the enemy move
- public GameObject shot;
- public Rigidbody2D rb;
- public float speed;
- //Check that the player is in the game
- public GameObject thePlayer;
- //Check that the Player isn't dead
- public bool thePlayerLives;
- //Have the enemy shoot at the player's position; the movement code is in the shot itself so what we do is instantiate it at the firing point
- public GameObject fireShot;
- //Where are we shooting from?
- public Transform fireMouth;
- //How long to wait between firing
- public float rateOfFire;
- public float fireDelay;
- //Call the Player Lives function just in case the shot is instantiated after the player dies
- private GameController gc;
- void Awake(){
- GameObject gco = GameObject.FindGameObjectWithTag("GameController");
- gc = gco.GetComponent<GameController>();
- rb = GetComponent<Rigidbody2D>();
- rb = GetComponent<Rigidbody2D>();
- rb.velocity = (-transform.right * speed);
- gc.Invoke ("CheckLIves",0);
- StartCoroutine ("Shooter");
- }
- void FixedUpdate () {
- gc.Invoke ("CheckLIves",0);
- }
- //Coroutine for spawning and shooting objects
- IEnumerator Shooter() {
- while (gc.thePlayerLives) {
- // print ("Coroutine Started");
- yield return new WaitForSeconds (fireDelay);
- gc.Invoke ("CheckLIves",0);
- if (gc.thePlayerLives) {
- for (int i = 0; i < 1; i++) {
- rb.velocity = Vector2.zero;
- yield return new WaitForSeconds (fireDelay);
- Instantiate (fireShot, fireMouth.position, fireMouth.rotation);
- rb.velocity = (-transform.right * speed);
- yield return new WaitForSeconds(rateOfFire);
- }
- }
- yield return new WaitForSeconds(rateOfFire);
- if (!gc.thePlayerLives) {
- rb.velocity = (-transform.right * speed);
- break;
- }
- }
- }
- }
- //===============//
- public class Enemy2Control : MonoBehaviour {
- public Rigidbody2D rb;
- public float speed;
- //Have the enemy shoot at the player's position; the movement code is in the shot itself so what we do is instantiate it at the firing point
- public GameObject fireShots;
- //Where are we shooting from?
- public Transform fireBig;
- public Transform fireLong;
- //How long to wait between firing
- public float rateOfFireBig;
- public float rateOfFireLong;
- public float fireDelay;
- void Start () {
- rb = GetComponent<Rigidbody2D>();
- rb.velocity = (-transform.right * speed);
- InvokeRepeating ("BigGuns",fireDelay,rateOfFireBig);
- InvokeRepeating ("LongGuns",fireDelay,rateOfFireLong);
- }
- void BigGuns() {
- if (Time.time > rateOfFireBig) {
- Instantiate (fireShots, fireBig.position, fireBig.rotation);
- }
- }
- void LongGuns() {
- if (Time.time > rateOfFireLong) {
- Instantiate (fireShots, fireLong.position, fireLong.rotation);
- }
- }
- }
- //===============//
- public class Enemy2DestroyOnContact : MonoBehaviour {
- //Declare a scorevalue for the object this is attached to
- public int scoreVal;
- //Call the GameController Script
- private GameController gc;
- void Awake (){
- GameObject gco = GameObject.FindGameObjectWithTag("GameController");
- gc = gco.GetComponent<GameController>();
- }
- void OnTriggerEnter2D(Collider2D other){
- if (other.CompareTag ("Weapon Shot")) {
- Destroy (other.gameObject);
- Destroy (gameObject);
- gc.AddScore (scoreVal);
- }
- }
- }
- //===============//
- public class EnemyShot2Control : MonoBehaviour {
- public GameObject shot;
- public Rigidbody2D rb;
- public float speed;
- // Use this for initialization
- void Start () {
- rb = GetComponent<Rigidbody2D>();
- rb.velocity = ((-transform.right) * speed);
- }
- }
- //===============//
- public class EnemyShotScript : MonoBehaviour {
- public GameObject shot;
- public Rigidbody2D rb;
- public float speed;
- public GameObject target;
- //private Transform shotTransform;
- private Vector3 targetPOS;
- //Call the Player Lives function just in case the shot is instantiated after the player dies
- private GameController gc;
- void Awake(){
- GameObject gco = GameObject.FindGameObjectWithTag("GameController");
- gc = gco.GetComponent<GameController>();
- rb = GetComponent<Rigidbody2D>();
- }
- void Start () {
- if (gc.thePlayerLives) {
- BeginShot ();
- } else
- Destroy (gameObject);
- }
- void BeginShot(){
- //Theoretically this finds the position of the player
- target = GameObject.FindGameObjectWithTag("Player");
- targetPOS = new Vector3 (target.transform.position.x, target.transform.position.y, target.transform.position.z);
- transform.rotation = Quaternion.FromToRotation (transform.up, targetPOS - transform.position) * transform.rotation;
- rb.velocity = (transform.up * speed);
- //Taken from Unity Answers, we need to break this down and figure out why it works
- //transform.rotation = Quaternion.FromToRotation (transform.up, targetPOS - transform.position) * transform.rotation;
- }
- }
Add Comment
Please, Sign In to add comment