Last active
August 29, 2015 14:20
-
-
Save Jaandlu/71f1a3f14df43a523dad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class FormationController : MonoBehaviour { | |
public GameObject enemyPrefab; | |
public float width = 10; | |
public float height = 5; | |
public float speed; | |
public float padding; | |
private float xmin; | |
private float xmax; | |
private bool moveRight; | |
private bool moveLeft; | |
// Use this for initialization | |
void Start () { | |
foreach(Transform child in transform){ | |
GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject; | |
enemy.transform.parent = child; | |
float distance = transform.position.z - Camera.main.transform.position.z; | |
Vector3 leftMost = Camera.main.ViewportToWorldPoint(new Vector3( 0,0,distance)); | |
Vector3 rightMost = Camera.main.ViewportToWorldPoint(new Vector3( 1,0,distance)); | |
xmin = leftMost.x + padding; | |
xmax = rightMost.x - padding; | |
} | |
moveLeft = true; | |
} | |
void OnDrawGizmos(){ | |
float gizmoWidth, gizmoHeight; | |
gizmoWidth = transform.position.x + 0.5f * width; | |
gizmoHeight = transform.position.y + 0.5f * height; | |
Gizmos.DrawWireCube(new Vector3(0,5,0), new Vector3(gizmoWidth, gizmoHeight,0 )); | |
} | |
// Update is called once per frame | |
void Update () { | |
checkMin(); | |
checkMax(); | |
if (moveLeft == true) { | |
transform.position += Vector3.left * speed * Time.deltaTime; | |
} | |
else if (moveRight == true) { | |
transform.position += Vector3.right * speed * Time.deltaTime; | |
} | |
float newX = Mathf.Clamp (transform.position.x, xmin, xmax); | |
transform.position = new Vector3(newX, transform.position.y, transform.position.z); | |
} | |
void checkMin() { | |
if (transform.position.x == xmin){ | |
moveLeft = false; | |
moveRight = true; | |
} | |
} | |
void checkMax() { | |
if (transform.position.x == xmax){ | |
moveRight = false; | |
moveLeft = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment