- Home /
Dynamic placement of AI
Yo
I did some searching around, but it did not yield anything use full.
I would really like to know how one is supposed to create a dynamic Instantiation system, which automatically finds the next set of spawnpoints around you and spawn AIs. Let me brake it down for you my friend:
Theory:
Player passes a trigger called "Place_AI"
Find near by spawn points(empty gameObjects)
Instantiate enemys of different types at said spawn points
However I am having some trouble figuring out the details I guess.
When the player passes the trigger, how would one find the spawn points for AIs close to the players position without "hard coding" it? And how would one easily tell the computer to spawn 2 of these AIs and 3 of these AIs?
What I have now in basic is this:
var count = 0;
var enemy : Transform;
function OnTriggerEnter (hit : Collider) {
if(hit.gameObject.tag == "Place_AI"){
count++;
PlaceAI(count);
}
}
function PlaceAI(count : int){
switch(count){
case count:
Instantiate(enemy, saidSpawnPoints.transform.position, transform.rotation);
break;
}
}
(I know there is a lot of redundancy, no need pointing that out ;))
But how would I find the spawns point relevant to my position dynamically? And how can I easily tell the computer to Instantiate x of these AIs and y of these AIs? While everything is as dynamic as possible(meaning not hard coded).
I hope you get what I am saying. I could really appreciate some help on this one.
Thank you.
For the exact locations of spawn points, I think you would have to 'hard code' it using empty transforms to designate appropriate places for AIs to spawn. This is to stop them from spawning in insane places like walls and in the air. Otherwise, it shoudn't be too hard to do it all in a trigger object which is connected to a bunch of spawn points- when you walk in the trigger, it makes things spawn, and then deactivates itself.
Also, you could have some kind of global registry of spawn points, and just find the 10 or so closest ones, that's another way of doing it.
Answer by DaveA · Oct 16, 2011 at 05:12 PM
What I do is have Spawnpoint objects (use the Editor to assign a nice icon for them), then assign them to an Array of Transform in my spawner script. There, you can pick random points, the 'next' point (round-robin), closest point, whatever. To find closest point, you loop through the array calling Vector3.Distance using the spawn point position from the array, and the current player position, keeping track of the smallest distance.
BTW You can also use the rotation of the spawnpoint object as the initial rotation of your spawned bot (or whatever). For this I have a gizmo that draws an arrow in that direction for ease of authoring.
Well how would you keep track of the smallest distance?
Your answer
![](https://koobas.hobune.stream/wayback/20220613011919im_/https://answers.unity.com/themes/thub/images/avi.jpg)