- Home /
Position an array of objects randomly
Hi guy, with the code below, I am able to load all assets from the asset bundle as an array and import them to the scene at run time, However, they all still spawn in the same position, which is (0,0,0). Now I want to spawn them at random positions that's at least 10 meters from each other, I've tried some for loops and stuffs, but they did not work out well, so I'm not sure how to go about this. Any tip would help, thank you!
IEnumerator DownloadAndLoadModels()
{
//Link to the host server
string url = "link to the server";
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
yield return request.SendWebRequest();
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
//Load all assets to the engine
GameObject[] allAssets = bundle.LoadAllAssets<GameObject>();
amountsOfAsset = new GameObject[allAssets.Length];
for (int i = 0; i < allAssets.Length; i++)
{
amountsOfAsset[i] = Instantiate(allAssets[i]) as GameObject;
}
}
Answer by ShadyProductions · Jun 11, 2020 at 07:19 AM
var random = new Random();
const int min = 10;
const int max = 100;
int previousX = 0;
int previousY = 0;
int previousZ = 0;
for (int i = 0; i < allAssets.Length; i++)
{
var newX = previousX + random.Next(min, max+1);
var newY = previousY + random.Next(min, max+1);
var newZ = previousZ + random.Next(min, max+1); //remove this if 2D
amountsOfAsset[i] = Instantiate(allAssets[i]) as GameObject;
amountsOfAsset[i].transform.position = new Vector3(newX, newY, newZ);
previousX = newX;
previousY = newY;
previousZ = newZ;
}
I thought this was the more simple part :) Note that this one only goes in positive coordinates starting from 0,0,0
What you can also do is grab a random position and then check all other spawn positions you already have and do a distance check to see if its >= 10 but that depends more on randomness so it can be less performant.
const int min = -500;
const int max = 500;
List<Vector3> spawnPositions = new List<Vector3>(amountsOfAsset.Length);
foreach (asset in amountsOfAsset)
{
Vector3 pos = new Vector3(random.Next(min,max), random.Next(min,max), random.Next(min,max));
while (spawnPositions.Any(a => Vector3.Distance(a, pos) < 10))
pos = new Vector3(random.Next(min,max), random.Next(min,max), random.Next(min,max));
spawnPositions.Add(pos);
asset.transform.position = pos;
}
Something like this.. Note that you have to compensate with min and max based on how many assets you have. And may want to add some check to prevent infinite loop in while statement in case it cannot find a suitable position (there is a chance that it cannot find a suitable position in the while loop if u have many assets and little min/max space).
Thank you so much for your effort. This helps alot!!!
Your answer
Follow this Question
Related Questions
moving random objects to random positions 1 Answer
How do I change the position of an object on the screen randomly ? 0 Answers
What's the best way to check if an area is empty? 7 Answers
Spawn object at a random predeterminated(transform) position 1 Answer
Randomly instantiate objects from array without choosing the same item twice. 2 Answers