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.