Advertisement
carrot_soup

Untitled

Feb 17th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemySpawner : MonoBehaviour {
  5. public GameObject enemyPrefab;
  6. public float width = 10f;
  7. public float height = 5f;
  8. private bool movingRight = true;
  9. public float speed = 5f;
  10. private float xmax;
  11. private float xmin;
  12.  
  13.  
  14. // Use this for initialization
  15. void Start () {
  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. xmax = rightBoundary.x;
  20. xmin = leftBoundary.x;
  21.  
  22. foreach( Transform child in transform){
  23. GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
  24. enemy.transform.parent = child;
  25. }
  26. }
  27.  
  28. public void OnDrawGizmos(){
  29. Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
  30. }
  31. // Update is called once per frame
  32. void Update(){
  33. if(movingRight){
  34. transform.position += Vector3.right * speed * Time.deltaTime;
  35. }else{
  36. transform.position += Vector3.left * speed * Time.deltaTime;
  37. }
  38.  
  39. float rightEdgeOfFormation = transform.position.x + (0.5f*width);
  40. float leftEdgeOfFormation = transform.position.x - (0.5f*width);
  41. if(leftEdgeOfFormation < xmin){
  42. movingRight = true;
  43. }else if(rightEdgeOfFormation > xmax){
  44. movingRight = false;
  45.  
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement