- Home /
Best way to find which two transforms in a list of transforms a point lies between.
I have a list of 7 transforms. They are in order from left to right (0 being the leftmost, 6 being the rightmost). The player walks around among them. I'd like to use a function to find out which two transforms the player is between on the x axis, or whether the player has gone beyond the leftmost or rightmost transform.
Just wondering what the best way to do this is? Am currently planning to loop through the list checking whether the player position is both right of an element of the list and left of that element + 1. i.e
if (player.position.x > transformList[i].position.x && player.position.x < transformList[i + 1].position.x)
It gets a bit messy when it comes to the player being left of the leftmost transform or right of the rightmost one, so I'm just wondering if there's a clever way someone knows?
Thank you very much! Romano
Your answer
Follow this Question
Related Questions
Finding Coordinates of Objects in a List 1 Answer
A node in a childnode? 1 Answer