- Home /
Array of Transforms with variable
I currently have AI that goes to formation slots, Right now they are hard set but I need them to figure out which spawn socket is free out of many of them (right now if 2 formations are in the scene they will only go to one formation). For instance I have a prefab called 4 slot formation, later on I'm going to randomly spawn these formations around the map, and my enemies go to the sockets inside these formations. Here is the code I have so far but its set to go to specific formations, I need it to be more dynamic.
Fighter_AI.cs
// Moves the ships to the right spots. May need to replace the target with a variable that gets adjusted by how many formations are added
void GoToSlot01()
{
transform.position = Vector3.Lerp(transform.position, FormTarg1.position, Time.deltaTime * smoothTranslateSpeed);
}
void GoToSlot02()
{
transform.position = Vector3.Lerp(transform.position, FormTarg2.position, Time.deltaTime * smoothTranslateSpeed);
}
void GoToSlot03()
{
transform.position = Vector3.Lerp(transform.position, FormTarg3.position, Time.deltaTime * smoothTranslateSpeed);
}
What I need to know how to do is set up a array of transforms that checks all the formations prefabs child spawn sockets and maybe turn that into a temp variable so after one fighter goes to 1 slot it then goes to the next transform. I've already asked the first part of this question on just reserving slots for the enemies but I don't know how to send them to each of the slots transform dynamically (they only go to 1 formation if 2 or more formations are in the scene). The other question is here.
http://answers.unity3d.com/questions/330696/unique-prefabs.html
Answer by sparkzbarca · Oct 13, 2012 at 07:47 AM
script attached to spawn prefab
public int formation size = 4 // must be public so you can see if a formation needs filled
bool[formation_size] fighters;
vector3[formation_size] FormTargets;
on start
iterate through fighter setting each to zero
foreach (transform child in transform)
{
foreach (bool plane in fighters}
{
position_iter++;
if(!plane)
{
transform.position = Vector3.Lerp(transform.position, FormTargets[position_iter].position, Time.deltaTime * smoothTranslateSpeed);
}
}
}
now all you have to do is set the fighters parent equal to the nearest formation with formation.transform.childcount < formation.formation_size;
fighter script
all_formations = FindObjectsOfType<formations>;
foreach (formations formation in all_formation)
{
if(formation.transform.childcount < formation.formation_size)
{
transform.parent = formation; //parent this fighter to that formation
}
}
took me a few minutes so mark answered if its answered and let me know if you need a little more help implementing. :P
Thanks that looks like it will work. Idk why but I usually post all day and don't get a answer till bed time. I'm pretty tired right now, I'll try this asap when I wake up. Do you $$anonymous$$d if I pm you if I run into trouble tomorrow?