Can someone show me a working example (preferably in JavaScript) of the hypothetical pseudo-code below?
var Target
for(GO:GameObject in GetObjectsInRange(gameObject,10 meters))
Target=GO
return
GetObjectsInRange is the hypothetical function that returns all game objects in the specified range around arg1, within a distance of arg2.
GetObjectsInRange is the function I need to see a working example of. It should return a list of the objects in the specified range, with the closest being the first in the list and ending with the furthest.
Ive no idea about javascript.
But one thing for sure is; you dont want to return all game objects. You want to have a static array list somewhere and you add the objects which you want to check in that array. Then in your function you check the distance to those objects and with a helper function you want to list those distances from shortest to longest.
Hope that helps.
Can you show me an efficient way to do that? Because detecting what objects are in certain ranges of the player is important in any game I can think of, yet I can’t find a working example to study. Can you show me one please?
Not looking at the reference right now, but I think the Physics class has a ‘sphere query’ function that you can use to find all the game objects within a specified range of another. (This assumes that said objects have colliders - if not, you’ll need to use another method.)
For sorting, I’d compute the squared distance to each object, and then use a function such as Array.Sort() to sort the objects by distance (there are some details involved with that approach, but you can always ask here if you get stuck on something).