I’ve recently posted this on Udemy Q&A for the course, but I figured I posted it here too.
Okay, for the life of me, I couldn’t figured this out.
Instead of having a preset number of position on my game screen. I wanted my game to randomly decide how many positions to instantiate into my Enemy Formation before instantiating my enemies. I have successfully completed this without them overlapping each other.
Unfortunately, my enemies don’t re-spawn after they all die.
It works find with preset positions, but not when I have the script randomly place them in my Enemy Formation.
The position are being instantiated as child(s) of the Enemy Formation, and my enemies are being instantiated as child(s) of the positions.
Whenever Update runs my AllMembersDead() method, it would always return false.
Eventually, I want my script to destroy all position and then recreate them before re-spawning new enemies. This way the enemies won’t always be in the same spot all the time. Which shouldn’t be hard for me to do, but first I need to get my re-spawn to work.
Oh, I’m using Unity 5.4.
Here is my code:
public GameObject enemyType;
public float width = 10f;
public float height = 5f;
public float speed = 5f;
public float spawnDelay = 0.5f;
public GameObject positionPrefab;
private bool movingRight = true;
private float xmax;
private float xmin;
private int spawnQuantity;
// Use this for initialization
void Start () {
CreatePos();
FormationSpace();
SpawnEnimes();
}
//Check If the Vector 3 Location Has Been Taken or Not
public bool IsPosAvailable(Vector3 spawnPos){
if(Physics.CheckSphere(spawnPos, 1f)){
return true;
}else {
return false;
}
}
//Generate a New Vector3 Coordinates until it finds a unused spot on the grid
public Vector3 GetNewPos(int x){
Vector3 newPos = new Vector3(Random.Range(-5.28f, 5.28f), x, 0);
if(!IsPosAvailable(newPos)){
GetNewPos(x);
return newPos;
} else {
return newPos;
}
}
void FormationSpace(){
float distanceToCamera = transform.position.z - Camera.allCameras[1].transform.position.z;
Vector3 leftBoundary = Camera.allCameras[1].ViewportToWorldPoint(new Vector3(0, 0, distanceToCamera));
Vector3 rightBoundary = Camera.allCameras[1].ViewportToWorldPoint(new Vector3(1, 0, distanceToCamera));
xmax = rightBoundary.x;
xmin = leftBoundary.x;
}
// Instantiate Positions into Enemy Formation
void CreatePos(){
spawnQuantity = Random.Range(5, 8);
for (int x = 1; x <= 1; x++){
for (int i = 1; i <= spawnQuantity; i++){
Vector3 newPos = new Vector3(Random.Range(-5.28f, 5.28f), x, 0);
if (!IsPosAvailable(newPos)){
GameObject Pos = Instantiate(positionPrefab, newPos, Quaternion.identity) as GameObject;
Pos.transform.parent = transform;
} else {
Vector3 newlyCreatedPos = GetNewPos(x);
GameObject Pos = Instantiate(positionPrefab, newlyCreatedPos, Quaternion.identity) as GameObject;
Pos.transform.parent = transform;
}
}
}
}
void SpawnEnimes() {
foreach (Transform child in transform){
GameObject enemy = Instantiate(enemyType, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
void SpawnUntilFull() {
Transform freePosition = NextFreePosition();
if (freePosition) {
GameObject enemy = Instantiate(enemyType, freePosition.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = freePosition;
}
if (NextFreePosition()) {
Invoke("SpawnUntilFull", spawnDelay);
}
}
public void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
}
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()){
FormationSpace();
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;
}