Advertisement
Ch0nG

Enemy

Aug 21st, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Enemy : MonoBehaviour {
  5.  
  6.     public Transform TS;
  7.     public float minSpeed = 3f, maxSpeed = 8f, currentSpeed;
  8.     public float llocation = -9.15f, rlocation = 9.15f, ylocation = 7.65f;
  9.  
  10.     // Use this for initialization
  11.     void Start () {
  12.  
  13.         TS = transform; //Cache Transform.
  14.  
  15.         currentSpeed = Random.Range(minSpeed, maxSpeed); //Randomize enemy speed;
  16.         TS.position = new Vector3 (Random.Range (llocation, rlocation), ylocation, 0); //Randomize enemy spawn point.
  17.     }
  18.    
  19.     // Update is called once per frame
  20.     void Update () {
  21.         TS.position += (Vector3.down * currentSpeed * Time.deltaTime); //Bring enemy down the screen.
  22.         if (TS.position.y < -5.35f) {
  23.             TS.position = new Vector3 (Random.Range (llocation, rlocation), ylocation, 0);
  24.             currentSpeed = Random.Range(minSpeed, maxSpeed);
  25.         } //end new spawnpoint and speed.
  26.     }
  27.  
  28.     void OnTriggerEnter(Collider collider)
  29.     {
  30.         if (collider.CompareTag("Laser") || collider.CompareTag("Player")) { //Tag is name of prefab.
  31.         //When the laser hits the enemy, destroy the laser and the enemy.
  32.             Destroy(this.gameObject);
  33.         }
  34.         if (Player.score < 500 || Player.playerLives >= 1) {
  35.             for (int enemies = 0; enemies < 3; enemies++) {
  36.             Vector3 nposition = new Vector3 (Random.Range (llocation, rlocation), ylocation, 0);
  37.             Instantiate(this, nposition, Quaternion.identity);
  38.             TS.position += (Vector3.down * currentSpeed * Time.deltaTime);
  39.             }
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement