Best way to handle detecting when in range to a list of objects?
Asked by
8 years ago Edited 8 years ago
Basically I'm creating a system where when you walk up to certain parts, if your character is within range
, a prompt will show up. For the sake of this question you don't have to worry about what the prompt will do, I'm just a bit confused on how I'd handle setting this up.
Info:
The method I have so far is creating a table, containing columns of other tables with information about the part the player needs to be within range of. Like this:
05 | Object = workspace.GreenStone |
11 | Object = workspace.RedStone |
16 | Object = workspace.BlueStone |
Now I like this way of storing the objects the player can interact with, but my method for CHECKING if the player is in range of them concerns me a bit.
I'm using magnitude
to obviously check the distance between the objects and the player, but the entire process is done within a while loop
, iterating the table and comparing the player's current position to each individual objects each iteration. This - I don't like. Here would be an example:
01 | function updateRange(player) |
02 | local torso = player.Character:WaitForChild( "Torso" ) |
05 | if (v.Object.Position-torso.Position).magnitude < v.Range then |
07 | print ( "Player is in range of: " , v.Name) |
This method seems buggy, impractical, inefficient, and I don't like it. So my question summed up is pretty much:
Is there a better way for this program to allow you to interact with the parts in the table when in range, without the need for a while loop updating the entire process each time?
If anyone could help with this, I'd really appreciate it. Thanks for reading.