hi
I had a couple of issues after adding the starfield, I think it is just a coincidence but thought stating that should be relevant.
One issue is : enemy spawner only spawns 3 out of 5.
2nd issue is: while in play mode enemy ships intermittently “shake” or look like they “vibrate”
using UnityEngine;
using System.Collections;
public class EnemySpawner : MonoBehaviour {
public GameObject enemyPrefab;
public float width = 10f;
public float height = 10f;
public float speed = 5f;
public float spawnDelay = 0.5f;
private bool movingRight = true;
private float xmax;
private float xmin;
// Use this for initialization
void Start () {
float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
Vector3 leftBoundry = Camera.main.ViewportToWorldPoint(new Vector3(0,0, distanceToCamera));
Vector3 rightBoundry = Camera.main.ViewportToWorldPoint(new Vector3(1,0, distanceToCamera));
xmax = rightBoundry.x;
xmin = leftBoundry.x;
SpawnUntilFull();
}
void SpawnEnemies() {
foreach( Transform child in transform) {
GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
void SpawnUntilFull(){
Transform freePosition = NextFreePosition();
if(freePosition){
GameObject enemy = Instantiate(enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
enemy.transform.parent = NextFreePosition();
}
if (NextFreePosition()){
Invoke ("SpawnUntilFull", spawnDelay);
}
}
public void OnDrawGizmos() {
Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
}
// Update is called once per frame
void Update () {
if (movingRight){
transform.position += Vector3.right*speed*Time.deltaTime;
}else{
transform.position += Vector3.left*speed*Time.deltaTime;
;
}
float rightEdgeOfFormation = transform.position.x + (0.5f*width);
float leftEdgeOfFormation = transform.position.x - (0.5f*width);
if (leftEdgeOfFormation < xmin){
movingRight = true;
}else if (rightEdgeOfFormation >xmax){
movingRight = false;
}
if(AllMembersDead()){
Debug.Log ("Empty Formation");
SpawnUntilFull();
}
}
Transform NextFreePosition(){
foreach(Transform childPositionGameObject in transform){
if (childPositionGameObject.childCount == 0){
return childPositionGameObject;
}
}
return null;
}
bool AllMembersDead(){
foreach(Transform childPositionGameObject in transform){
if (childPositionGameObject.childCount > 0){
return false;
}
}
return true;
}
}
using UnityEngine;
using System.Collections;
public class EnemyBehavior : MonoBehaviour {
public GameObject projectile;
public float projectileSpeed = 10;
public float health = 150;
public float shotsPerSeconds = 0.5f;
void Update(){
float probability = Time.deltaTime * shotsPerSeconds;
if(Random.value < probability){
Fire ();
}
}
void Fire() {
Vector3 startPosition = transform.position + new Vector3(0, -1, 0);
GameObject missile = Instantiate(projectile, startPosition, Quaternion.identity) as GameObject;
missile.rigidbody2D.velocity = new Vector2(0, -projectileSpeed);}
void OnTriggerEnter2D(Collider2D collider){
Projectile missile = collider.gameObject.GetComponent();
if (missile){
health -= missile.GetDamage();
missile.Hit();
if (health <= 0){
Destroy (gameObject);
}
}
}
}