- Home /
Select from an array based on values
I have an array of distances from the player to the enemy spawn point from which I am looking to pick out a value. Instead of choosing the closest value, I am looking to randomly select a distance based on its proximity, so if all the values were very similar, any could be chosen, where as if they were radically different, the chosen value would mostly be the closest value. Thanks!
I don't understand what you're trying to do. Can you explain why?
I'm spawning an enemy every 5 seconds from any one of the spawn points around my map. To avoid enemies only spawning from the closest point which the player can currently just camp around, I'm looking for some code which will add a chance that other spawn points will be chosen, with the likelihood of being chosen linked to their proximity.
Answer by roamcel · Oct 27, 2011 at 09:37 AM
You necessarily need to define a 'tolerance' value which defines 'proximity' and then, you necessarily need to scan the whole array every time.
So you can solve this by a sorting algorithm, or by simply scanning the array every iteration, picking out the nearest value, then picking a random value, and seeing if such value is 'close enough' or if you need to fall back to the nearest value.
Your answer
Follow this Question
Related Questions
Spawning targets on 2 of 4 pre-defined locations using an array and empty game objects? 1 Answer
Spawning Objects Using An Array. 1 Answer
Spawn object from Random Vector3 in array 0 Answers
How do I Spawn a random gameObject from Array1, at a random position of gameObjects from Array2 ? 1 Answer
How to fix some of location have more than 1 object? 2 Answers