[Solved] Enemy formation getting stuck on side of the screen

I don’t know what happened, but all of the sudden, when I changed my code so that enemies appear one at a time, it also had the unusual effect of triggering the bug where they get stuck on the side. I’m not sure what I did to cause this.

Here is my EnemySpawner code:

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {

public GameObject enemyPrefab;
public float width = 10f;
public float height = 5f;
public float enemySpeed = 0.1f;
public float spawnDelay = 0.5f;

private bool direction = false;
private float xMin;
private float xMax;

// Use this for initialization
void Start () {
	SpawnUntilFull();
}

public void OnDrawGizmos(){
	Gizmos.DrawWireCube(transform.position, new Vector3(width, height, 0));
}

// Update is called once per frame
void Update () {
	if(direction == true){
		transform.position += Vector3.right * enemySpeed * Time.deltaTime;
	} else if(direction == false){
		transform.position += Vector3.left * enemySpeed * Time.deltaTime;
	}

	float rightEdgeOfFormation = transform.position.x + 0.5f * width;
	float leftEdgeOfFormation = transform.position.x - 0.5f * width;

	if(leftEdgeOfFormation < xMin){
		direction = true;
	} else if(rightEdgeOfFormation > xMax){
		direction = false;
	}

	if(AllMembersDead()){
		Debug.Log("Empty Formation");
		SpawnUntilFull();
	}
}

void SpawnNewSquad(){
	float distanceToCamera = transform.position.z - Camera.main.transform.position.z;

	Vector3 leftEdge = Camera.main.ViewportToWorldPoint(new Vector3(0,0, distanceToCamera));
	Vector3 rightEdge = Camera.main.ViewportToWorldPoint(new Vector3(1,0, distanceToCamera));

	xMax = rightEdge.x;
	xMin = leftEdge.x;
	
	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 = freePosition;
	}
	if(NextFreePosition()){
		Invoke("SpawnUntilFull", spawnDelay);
	}
}

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;
}

}

Thank you for your help!

Does anyone have any idea what could be causing this? I rewatched the lecture about fixing that bug, and it only points to the enemies moving off the screen when you write it as a toggle function. But I wrote the code the way he demonstrated, so my enemies shouldn’t be going off the screen and getting stuck. Can anyone see why they would be, given this code?

Hi Eric,

If you want to zip the project up (assuming you’re using a PC) I will happily have a look for you.

Common issues are the toggle and also the size of the enemy formation.

Thank you! I will zip up the project later tonight and send it to you.

Should I just upload it to this forum?

1 Like

Yeah that’ll be fine, there is a max file size but I think it will be ok. If there are any problems let me know and we will find an alternative method.

Laser Defender.zip (4.8 MB)

Here it is. Thank you so much for your help! Sorry it took me a few days to get it up here.

1 Like

Hi Eric,

Ok, took a quick look.

In EnemySpawner.cs you have a private method called SpawnNewSquad(), nothing is currently calling this method, and unfortunately the following lines of code are in it and are the only lines of code which populate the variables xMin and xMax;

		float distanceToCamera = transform.position.z - Camera.main.transform.position.z;

		Vector3 leftEdge = Camera.main.ViewportToWorldPoint(new Vector3(0,0, distanceToCamera));
		Vector3 rightEdge = Camera.main.ViewportToWorldPoint(new Vector3(1,0, distanceToCamera));
		xMax = rightEdge.x;
		xMin = leftEdge.x;

As a very quick test I pasted the above directly into the Start() method, before the SpawnUntilFull(); method call and the behaviour of the enemy ships moving backwards and forwards was as expected.

Hope this helps :slight_smile:

Yes, thank you! You’re a lifesaver! I switched my call of SpawnUntilFull() in the Start method to SpawnNewSquad() and that made them move correctly. :slight_smile:

1 Like

You’re more than welcome :slight_smile: