[SOLVED]Enemy Objects start "sticking" together after initial movement to the right

Hi, ive been experiencing a problem where the prefabs of enemy start overlapping each other and moving together instead of maintaining the arrangement i sorted them into. I’ve been trying to solve this problem for quite some time but i am unable to isolate the issue. I would appreciate it if someone could help me out with this problem.
this is the screen shot of the problem:

this is my code for enemyspawner :

public class EnemySpawner : MonoBehaviour {

	public GameObject enemyPrefab;
	public float width =10f;
	public float height = 5f;
	public float speed = 5f;

	private float xmin ;
	private float xmax;
	private bool movingRight = true;

	// Use this for initialization
	void Start () {
		// get the sides of the screen
		float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
		Vector3 leftBoundary = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distanceToCamera));
		Vector3 rightBoundary = Camera.main.ViewportToWorldPoint (new Vector3 (1,0,distanceToCamera));
		xmax = rightBoundary.x;
		xmin = leftBoundary.x;

		//add enemy ontop of every position in the enemy spawner game object
		foreach (Transform child in transform) {
			//instantiate enemy
			GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
			//make enemy spawn under the enemy formation parent object in the game to tidy up game
			enemy.transform.parent = child;
		}


	}

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

		//compare xmax and xmin to current position, transform.position is center so need reference right and left positions
		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;
		}
	}
}

The issue was caused by the enemy spawner script added to enemy by accident. I hope no one else runs into this silly issue…

Privacy & Terms