Hey there,
I’m posting my code here because I’ve got to dead end and after rewriting my code twice, I think I’m missing something, so I came for help.
Basically, you call a method which generates the amount of objects (viruses) at the start of the game, but somehow when I want to position them not to overlap each other, the calculation doesn’t work.
You call this method like this: GL.InstantiateVirus(int quantity, string Virus)
I call this method 3 times with 3 different viruses but it doesn’t seem to work. Could someone take a look at it what’s wrong with my calculation?
Thank you in advance!
public class GenerateLevel : MonoBehaviour {
public List<StartVirus> VirusList;
public List<StartVirus> VirusListAll;
// Use this for initialization
void Start () {
VirusList = new List<StartVirus> ();
VirusListAll = new List<StartVirus> ();
}
// Update is called once per frame
void Update () {
}
public void InstantiateVirus(int quantity, string virus) {
//Store the positions as Vector3 as ranges where X = width, Y = height, Z = Distance from camera
//from 0-1. 0 is bottom left of camera, 1 is top right of the camera.
VirusList.Clear();
for (int i = 0; i < quantity; i++) {
StartVirus _virus = new StartVirus();
_virus.Pos = new Vector3(Random.Range(1f,100f) / 100f, Random.Range(1f,100f) / 100f, Globals.cameraHeight);
_virus.Virus = Resources.Load ("Virus " + virus.ToString ()) as GameObject;
_virus.Bounds = _virus.Virus.GetComponent<BoxCollider>().size / 100f;
Debug.Log(_virus.Bounds);
//Check if there's no previous position which _virus wants to take
bool positionGood = false;
while (!positionGood) {
for (int j = 0; j < VirusListAll.Count; j++) {
StartVirus _checkVirus = VirusListAll[j];
if (_checkVirus.Pos.x - _checkVirus.Bounds.x > _virus.Pos.x + _virus.Bounds.x &&
_checkVirus.Pos.x + _checkVirus.Bounds.x < _virus.Pos.x - _virus.Bounds.x &&
_checkVirus.Pos.z - _checkVirus.Bounds.z > _virus.Pos.z + _virus.Bounds.z &&
_checkVirus.Pos.z + _checkVirus.Bounds.z < _virus.Pos.z - _virus.Bounds.z) {
continue;
} else {
_virus.Pos = new Vector3(Random.Range(1f,100f) / 100f, Random.Range(1f,100f) / 100f, Globals.cameraHeight);
Debug.Log("Generated New Position");
break;
}
}
positionGood = true;
}
Debug.Log(_virus.Virus);
VirusList.Add (_virus);
VirusListAll.Add(_virus);
}
for (int i = 0; i < quantity; i++) {
GameObject _go = Instantiate (VirusList_.Virus, Camera.main.ViewportToWorldPoint (VirusList *.Pos), Quaternion.identity) as GameObject;*_
* go.name = virus.ToString ();
_ }*
* }*
[System.Serializable]
public class StartVirus {
[SerializeField]
private Vector3 pos;
public Vector3 Pos { get { return pos; } set { this.pos = value; } }
[SerializeField]
private Vector3 bounds;
public Vector3 Bounds { get { return bounds; } set { this.bounds = value; } }
[SerializeField]
private GameObject virus;
public GameObject Virus { get { return virus; } set { this.virus = value; } }
}
}