Advertisement
Guest User

EnemySpawner.cs

a guest
Mar 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. Public class EnemySpawner : MonoBehaviour
  2. {
  3.     public GameObject enemyPrefab;
  4.     public float width = 10;
  5.     public float height = 5;
  6.     public float speed = 10f;
  7.  
  8.     private bool movingRight = true;
  9.     private float maxX;
  10.     private float minX;
  11.  
  12.     // Use this for initialization
  13.     void Start ()
  14.     {
  15.         // Calculate the Boundary of the screen
  16.         float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
  17.         Vector3 leftBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distanceToCamera));
  18.         Vector3 rightBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distanceToCamera));
  19.         maxX = rightBoundary.x;
  20.         minX = leftBoundary.x;
  21.        
  22.         // Loop through every item in the collection to create an enemy on the positions.
  23.         foreach (Transform child in transform)
  24.         {
  25.             GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
  26.             enemy.transform.parent = child;
  27.         }
  28.     }
  29.  
  30.     public void OnDrawGizmos()
  31.     {
  32.         Gizmos.DrawWireCube(transform.position, new Vector3(width, height, 0));
  33.     }
  34.  
  35.     // Update is called once per frame
  36.     void Update()
  37.     {
  38.         // Check if the formation is going outside of the play space.
  39.         float rightEdgeOfFormation = transform.position.x + (0.5f * width);
  40.         float leftEdgeOfFormation = transform.position.x - (0.5f * width);
  41.  
  42.         // Set the formation to move left or right between the limits of the edges
  43.         if (movingRight)
  44.         {
  45.             transform.position += Vector3.right * speed * Time.deltaTime;
  46.         }
  47.         else
  48.         {
  49.             transform.position += Vector3.left * speed * Time.deltaTime;
  50.         }
  51.  
  52.         // Switch the direction of the formation
  53.         if (leftEdgeOfFormation < minX)
  54.         {
  55.             movingRight = true;
  56.         }
  57.         else if (rightEdgeOfFormation > maxX)
  58.         {
  59.             movingRight = false;
  60.         }
  61.  
  62.         if (AllMembersDead())
  63.         {
  64.             Debug.Log("Empty formation!");
  65.         }
  66.     }
  67.  
  68.     bool AllMembersDead()
  69.     {
  70.         foreach (Transform childPositionGameObject in transform)
  71.         {
  72.             if (childPositionGameObject.childCount > 0)
  73.             {
  74.                 return false;
  75.             }
  76.         }
  77.         return true;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement