Enemies spawn inside of each other

I’m making a simple running game, in which enemies are always coming at you. Their speed is based off of the rotation rate of a cylinder that I have (to produce to “rolling log” effect). The main problem I am having is that the enemies are spawning on top of each other whenever the RandomPosition() function is called… What can I do with my script to fix this?

Here is the enemy script:

``````using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemyScript : MonoBehaviour {

//Private, used only in this class.
private float x,y,z;
private float zAxis;
public WorldRotate worldRotate;
public GameObject[] enemies;

//Public, shows in inspector

//Offset to be spawned within the players perspective
public float xOffsetL = 35f,
xOffsetR = 35f,
yOffset = 51.5f,
zOffsetN = 10,
zOffsetF = 15f;

public float RotationSpeedIncrement = 0.001f;

public float MaxZAxis = -45;
public float MinZAxis = -30;
//Use before objects are drawn.
void Awake(){

worldRotate = GameObject.Find("Ocean").GetComponent<WorldRotate>();
}

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

// Update is called once per frame
void Update ()
{
zAxis = transform.position.z;
if (zAxis <= Random.Range(MaxZAxis,MinZAxis))
{
RandomPosition();
worldRotate.rotationRate = worldRotate.rotationRate + RotationSpeedIncrement;

}

}

//Assign a random position along the X axis, but within the players perspective.
void RandomPosition()
{
transform.rotation = Quaternion.identity;
x = Random.Range(PlayerMove.xAxis - xOffsetL , PlayerMove.xAxis + xOffsetR);
y = yOffset;
z = Random.Range (zOffsetN,zOffsetF);
transform.position = new Vector3(x,y,z);
}

void OnTriggerEnter(Collider otherObject){
if (otherObject.tag == "Player"){
foreach(var item in enemies)
{
Destroy(item.gameObject);
}
}
}
}
``````

PS: I know this question has been asked a million times, but I need an answer specific to my RandomPosition() function.

Seems quite right to me… hmmm

You would have to cycle through all the enemies and players and get a position that is outside of them. If there are so many that this is a problem, then you may have to resort to other methods.

How would I go about getting a reference to their spawned positions? What I have is an array that holds all enemies. If the designer (me) checks the bool to be true, then they are instantiated… It sounds like an array would have to be implemented to hold all of their transforms, but I don’t know exactly how to put all of these transforms into an array with the way that I am instantiating them… Here is my script:

``````	public void InstantiatePrefabs ()
{
if (UseEnemy) {
foreach (GameObject GO in EnemyPrefab) {
GameObject GO1 = Instantiate (GO) as GameObject;
GO1.transform.parent = ParentGameObject.transform;
}

}

if (UseEnemy1) {
foreach (GameObject GO in EnemyPrefab1) {
GameObject GO1 = Instantiate (GO) as GameObject;
GO1.transform.parent = ParentGameObject.transform;
}

}

if (UseEnemy2) {
foreach (GameObject GO in EnemyPrefab2) {
GameObject GO1 = Instantiate (GO) as GameObject;
GO1.transform.parent = ParentGameObject.transform;
}
}

if (UsePowerUps) {
foreach (GameObject GO in PowerUps) {
GameObject GO1 = Instantiate (GO) as GameObject;
GO1.transform.parent = ParentGameObject.transform;
}
}

if (UseCurrency) {
foreach (GameObject GO in Currency) {
GameObject GO1 = Instantiate (GO) as GameObject;
GO1.transform.parent = ParentGameObject.transform;
}
}

if (InstantiatePlayer) {
//Instantiate Player
Instantiate (PlayerPrefab);
Time.timeScale = 1.0f;
}
}
``````

This is like… The last thing I have to fix before we can start beta testing.