Question by
Galatia410 · Dec 30, 2017 at 02:19 AM ·
scripting problempositionscripting beginnerscriptingbasicsdistance
randomly Instantiate items
I'm trying to Instantiate multiple enemy spawn points at random locations when my level loads, making sure they don't overlap within a certain radius, of each other, main character, and a boss area. everything is almost working great but from what i can tell nothing is being spawned to the right or down from any of the other already placed objects. its also taking 30sec to 1 min to calculate the 20 spawn points... If anyone can help with this issue it would be greatly appreciated. I'm also attacking a screenshot of whats happening. the white dots are placeholders for the enemy spawnpoints.
Varriable to check if valid position:
static public bool CheckIfInSquare(Vector3 NewObjectPosition, Vector3 OldObjectsPosition, float RadiusBetweenObjects){
if (NewObjectPosition.x < OldObjectsPosition.x - RadiusBetweenObjects || NewObjectPosition.x > OldObjectsPosition.x + RadiusBetweenObjects
&& NewObjectPosition.z < OldObjectsPosition.z - RadiusBetweenObjects || NewObjectPosition.z > OldObjectsPosition.z + RadiusBetweenObjects){
return true;
} else{
return false;
}
}
Level Script
// Public
public GameObject BossArea01;
public GameObject SpawnPointMarker01;
// Private
private Vector3 BossArea01Position;
private GameObject Spawnpoint01;
private GameObject Spawnpoint02;
private GameObject Spawnpoint03;
private GameObject Spawnpoint04;
private GameObject Spawnpoint05;
private GameObject Spawnpoint06;
private GameObject Spawnpoint07;
private GameObject Spawnpoint08;
private GameObject Spawnpoint09;
private GameObject Spawnpoint10;
private GameObject Spawnpoint11;
private GameObject Spawnpoint12;
private GameObject Spawnpoint13;
private GameObject Spawnpoint14;
private GameObject Spawnpoint15;
private GameObject Spawnpoint16;
private GameObject Spawnpoint17;
private GameObject Spawnpoint18;
private GameObject Spawnpoint19;
private GameObject Spawnpoint20;
private int SummonSpawnPointInt = 1;
// ******************************** Start *****************************************
void Start () {
// Randomly Select Boss Area Position
int rand = Random.Range (1, 5);
if (rand == 1){
BossArea01Position = new Vector3 (150, .02f, 150);
} else if (rand == 2){
BossArea01Position = new Vector3 (-150, .02f, -150);
} else if (rand == 3){
BossArea01Position = new Vector3 (150, .02f, -150);
} else if (rand == 4){
BossArea01Position = new Vector3 (-150, .02f, 150);
}
// Summon Boss Area
Instantiate (BossArea01, BossArea01Position, Quaternion.identity);
}
// ******************************* Update *****************************************
void Update () {
// Randomly Summon SpawnPointMarker
if (SummonSpawnPointInt <= 20){
Vector3 position = new Vector3 (Random.Range (-175, 175), 1, Random.Range (-175, 175));
if (Galatia420.CheckIfInSquare (position, Character01Movement.MainCharacterPosition, 20)){//20
if (Galatia420.CheckIfInSquare (position, BossArea01Position, 35)){//35
if (Spawnpoint01 == null || Galatia420.CheckIfInSquare (position, Spawnpoint01.transform.position, 10))//20
if (Spawnpoint02 == null || Galatia420.CheckIfInSquare (position, Spawnpoint02.transform.position, 10))
if (Spawnpoint03 == null || Galatia420.CheckIfInSquare (position, Spawnpoint03.transform.position, 10))
if (Spawnpoint04 == null || Galatia420.CheckIfInSquare (position, Spawnpoint04.transform.position, 10))
if (Spawnpoint05 == null || Galatia420.CheckIfInSquare (position, Spawnpoint05.transform.position, 10))
if (Spawnpoint06 == null || Galatia420.CheckIfInSquare (position, Spawnpoint06.transform.position, 10))
if (Spawnpoint07 == null || Galatia420.CheckIfInSquare (position, Spawnpoint07.transform.position, 10))
if (Spawnpoint08 == null || Galatia420.CheckIfInSquare (position, Spawnpoint08.transform.position, 10))
if (Spawnpoint09 == null || Galatia420.CheckIfInSquare (position, Spawnpoint09.transform.position, 10))
if (Spawnpoint10 == null || Galatia420.CheckIfInSquare (position, Spawnpoint10.transform.position, 10))
if (Spawnpoint11 == null || Galatia420.CheckIfInSquare (position, Spawnpoint11.transform.position, 10))
if (Spawnpoint12 == null || Galatia420.CheckIfInSquare (position, Spawnpoint12.transform.position, 10))
if (Spawnpoint13 == null || Galatia420.CheckIfInSquare (position, Spawnpoint13.transform.position, 10))
if (Spawnpoint14 == null || Galatia420.CheckIfInSquare (position, Spawnpoint14.transform.position, 10))
if (Spawnpoint15 == null || Galatia420.CheckIfInSquare (position, Spawnpoint15.transform.position, 10))
if (Spawnpoint16 == null || Galatia420.CheckIfInSquare (position, Spawnpoint16.transform.position, 10))
if (Spawnpoint17 == null || Galatia420.CheckIfInSquare (position, Spawnpoint17.transform.position, 10))
if (Spawnpoint18 == null || Galatia420.CheckIfInSquare (position, Spawnpoint18.transform.position, 10))
if (Spawnpoint19 == null || Galatia420.CheckIfInSquare (position, Spawnpoint19.transform.position, 10))
if (Spawnpoint20 == null || Galatia420.CheckIfInSquare (position, Spawnpoint20.transform.position, 10))
// Summon
switch (SummonSpawnPointInt){
case 1:
Spawnpoint01 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 2:
Spawnpoint02 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 3:
Spawnpoint03 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 4:
Spawnpoint04 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 5:
Spawnpoint05 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 6:
Spawnpoint06 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 7:
Spawnpoint07 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 8:
Spawnpoint08 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 9:
Spawnpoint09 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 10:
Spawnpoint10 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 11:
Spawnpoint11 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 12:
Spawnpoint12 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 13:
Spawnpoint13 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 14:
Spawnpoint14 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 15:
Spawnpoint15 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 16:
Spawnpoint16 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 17:
Spawnpoint17 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 18:
Spawnpoint18 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 19:
Spawnpoint19 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
break;
case 20:
Spawnpoint20 = Instantiate (SpawnPointMarker01, position, Quaternion.identity);
SummonSpawnPointInt += 1;
Debug.Log ("Level01: Done Summoning Spawnpoints");
break;
}
}
}
}
}
p.s. if anyone has a good suggestion on how to shorten this... lol i'm up for suggestions ;) .
capture.png
(375.2 kB)
Comment